title: Drupal PHP serialize and unserialize
body: <p>In Drupal, I first serialized emails that appear in the body of private messages and stored them is MySQL like this:</p>
  
  <pre><code>function prvtmsg_list($body) {
    $notify = array();
    if (isset($body-&gt;emails)) {
      $notify['mid'] = $body-&gt;mid;
      $notify['emails'] = serialize($body-&gt;emails);
    }
    if (isset($body-&gt;vulgar_words) {
      $notify['mid'] = $body-&gt;mid;
      $notify['vulgar_words'] = serialize($message-&gt;vulgar_words);
    }
    if (isset($notify['mid'])) {
      drupal_write_record('prvtmsg_notify', $notify);
    }
  }
  </code></pre>
  
  <p>When I later try to retrieve them, email userialization fails, I retrieve them like this:</p>
  
  <pre><code>function prvtmsg_list_notify() {
    // Select fields from prvtmsg_notify and Drupal pm_message tables
    $query = db_select('prvtmsg_notify', 'n');
    $query-&gt;leftJoin('pm_message', 'm', 'n.mid = m.mid');
    $query-&gt;fields('n', array('mid', 'emails', 'vulgar_words'));
    $query-&gt;fields('m', array('mid', 'author', 'subject', 'body', 'timestamp'));
    orderBy('timestamp', 'DESC');
    $query = $query-&gt;extend('PagerDefault')-&gt;limit(20);
    $result = $query-&gt;execute()-&gt;fetchAll();
  
    $rows = array();
    foreach ($result as $notify) {
      $rows[] = array(
        $notify-&gt;author,
        $notify-&gt;subject,
        implode(', ', unserialize($notify-&gt;emails)),
        implode(', ', unserialize($notify-&gt;vulgar_words)),
      );
    }
  
    $build = array();
    $build['table'] = array(
      '#theme' =&gt; 'table',
      '#header' =&gt; array(
        t('Author'),
        t('Message subject'),
        t('Emails captured'),
        t('Vulgar Words Captured'),
      ),
      '#rows' =&gt; $rows,
    );
    $build['pager']['#theme'] = 'pager';
  
    return $build;
  </code></pre>
  
  <p>}</p>
  
  <p>Maybe the way I serialized the emails is wrong? because:</p>
  
  <pre><code>dpm(unserialize($notify-&gt;emails);
  </code></pre>
  
  <p>gives Array, Array, Array - which means:</p>
  
  <p><code>Array(
    [0] =&gt; Array() [1] =&gt; Array() [2] =&gt; Array() [3] =&gt; Array()
  )</code></p>
  
  <p>Surprisingly, the unserialized vulgar words are showing okay! I'm not sure is it possible to serialize the emails like this:</p>
  
  <pre><code>$notify['emails'] = serialize (array($body-&gt;emails));
  </code></pre>
  
  <p>I faced the exact situation in the past where unserialization did not work for me, there is something not clear to me and I need to learn it. Could anyone confirm or tell me what's wrong?</p>
  
  <p>N.B. The above code is from memory and may not be accurate as I currently don't have access to it.</p>