title: PHP - Creating a comma separated list within foreach
body: <p>I was able to group an array by a common key using <code>foreach</code> now I am looking to take further and create a comma delimited list and each try results in a list but with no commas; they are either getting removed or I am not doing this right.</p>
  
  <pre><code>$families = array();
  foreach($results as $key =&gt; $item) {
      $families[$item['family_id']][] = $item;
  }
  
  Array (
      [54] =&gt; Array (
              [0] =&gt; Array (
                      [family_id] =&gt; 54
                      [firstname] =&gt; Adam
                      [primary] =&gt; 1
                  )
          )
      [89] =&gt; Array (
              [0] =&gt; Array (
                      [family_id] =&gt; 89
                      [firstname] =&gt; Alex
                      [primary] =&gt; 1
                  )
          )
      [44] =&gt; Array (
              [0] =&gt; Array (
                      [family_id] =&gt; 44
                      [firstname] =&gt; Anne-Marie
                      [primary] =&gt; 1
                  )
          )
      [282] =&gt; Array (
              [0] =&gt; Array (
                      [family_id] =&gt; 282
                      [firstname] =&gt; Ashley
                      [primary] =&gt; 1
                  )
              [1] =&gt; Array (
                      [family_id] =&gt; 282
                      [firstname] =&gt; Leah
                      [primary] =&gt; 
                  )
              [2] =&gt; Array (
                      [family_id] =&gt; 282
                      [firstname] =&gt; Brad
                      [primary] =&gt; 
                  )
          )
      [292] =&gt; Array (
              [0] =&gt; Array (
                      [family_id] =&gt; 292
                      [firstname] =&gt; Brenda
                      [primary] =&gt; 1
                  )
          )
      )
  </code></pre>
  
  <p>The end result is a <code>foreach</code> the generates the following list of families with children.</p>
  
  <p>Family: Adam<br><br>
  Family: Alex<br><br>
  Family: Anne-Marie<br><br>
  Family: Ashley<br>
  Children: Leah, Brad<br><br>
  Family: Brenda</p>
  
  <pre><code>foreach($families as $family) {
    foreach($family as $key =&gt; $value) {
      if (empty($value['primary'])) {
        $children = array('firstname' =&gt; $value['firstname']);
        $child = implode(', ', $children);
      }
      if (!empty($value['primary'])) {
        echo 'Family: '.$value['firstname'];
      }
    }
  }
  </code></pre>