title: PHP Array Parsing
body: <p>Hey all, I have a huge array coming back as search results and I want to do the following:</p>
  
  <p>Walk through the array and for each record with the same "spubid" add the following keys/vals: "sfirst, smi, slast" to the parent array member in this case, $a[0].  So the result would be leave $a[0] in tact but add to it, the values from sfirst, smi and slast from the other members in the array (since they all have the same "spubid").  I think adding the key value (1, 2, 3) to the associate key (sfirst1=> "J.", smi1=>"F.", slast1=>"Kennedy") would be fine.  I would then like to DROP (unset()) the rest of the members in the array with that "spubid".  Here is a simplified example of the array I am getting back and in this example all records have the same "spubid":</p>
  
  <pre><code>Array ( 
    [0] =&gt; 
      Array ( 
        [spubid] =&gt; A00502 
        [sfirst] =&gt; J. 
        [smi] =&gt; A. 
        [slast] =&gt; Doe
    [1] =&gt; 
      Array ( 
        [spubid] =&gt; A00502 
        [sfirst] =&gt; J. 
        [smi] =&gt; F. 
        [slast] =&gt; Kennedy 
    [2] =&gt; 
      Array ( 
        [spubid] =&gt; A00502 
        [sfirst] =&gt; B. 
        [smi] =&gt; F. 
        [slast] =&gt; James 
    [3] =&gt; 
      Array ( 
        [spubid] =&gt; A00502
        [sfirst] =&gt; S. 
        [smi] =&gt; M. 
        [slast] =&gt; Williamson 
        )
      )
  </code></pre>
  
  <p>So in essence I want to KEEP $a[0] but add new keys=>values to it (sfirst$key, smi$key, slast$key) and append the values from "sfirst, smi, slast" from ALL the members with that same "spubid" then unset $a[1]-[3].</p>
  
  <p>Just to clarify my IDEAL end result would be:</p>
  
  <pre><code>Array ( 
    [0] =&gt; 
      Array ( 
        [spubid] =&gt; A00502 
        [sfirst] =&gt; J. 
        [smi] =&gt; A. 
        [slast] =&gt; Doe
        [sfirst1] =&gt; J.
        [smi1] =&gt; F. 
        [slast1] =&gt; Kennedy
        [sfirst2] =&gt; B. 
        [smi2] =&gt; F. 
        [slast2] =&gt; James 
        [sfirst3] =&gt; S. 
        [smi3] =&gt; M. 
        [slast3] =&gt; Williamson
      )
    )
  </code></pre>
  
  <p>In most cases I will have a much bigger array to start with that includes a multitude of "spubid"'s but 99% of publications have more than one author so this routine would be very useful in cleaning up the results and making the parsing process for display much easier.</p>
  
  <p>***UPDATE</p>
  
  <p>I think by over simplifying my example I may have made things unclear.  I like both Chacha102's and zombat's responses but my "parent array" contains A LOT more than just a pubid, that just happens to be the primary key.  I need to retain a lot of other data from that record, a small portion of that is the following:</p>
  
  <pre><code>[spubid] =&gt; A00680 
  [bactive] =&gt; t 
  [bbatch_import] =&gt; t 
  [bincomplete] =&gt; t 
  [scitation_vis] =&gt; I,X 
  [dentered] =&gt; 2009-08-03 12:34:14.82103 
  [sentered_by] =&gt; pubs_batchadd.php 
  [drev] =&gt; 2009-08-03 12:34:14.82103 
  [srev_by] =&gt; pubs_batchadd.php 
  [bpeer_reviewed] =&gt; t 
  [sarticle] =&gt; A case study of bora-driven flow and density changes on the Adriatic shelf (January 1987)
  .
  .
  .
  .
  .
  </code></pre>
  
  <p>There are roughly 40 columns that come back with each search query.  Rather than hard coding them as these examples do with the pubid, how can I include them while still making the changes as you both suggested.  Creating a multi-dimensional array (as both of you suggested) with the authors being part of the multi-dimension is perfectly fine, thank you both for the suggestion.</p>
  
  <p>**** UPDATE:</p>
  
  <p>Here is what I settled on for a solution, very simple and gets the job done nicely.  I do end up creating a multi-dimensional array so the authors are broken out there.</p>
  
  <p>Over simplified solution:</p>
  
  <pre><code>$apubs_final = array();
  $spubid = NULL;
  $ipub = 0;
  
  foreach($apubs as $arec)
  {
    if($spubid != $arec['spubid'])
    {
      $ipub++;
      $apubs_final[$ipub] = $arec;
      // insert UNSET statements here for author data
      $iauthor = 0;
      $spubid = $arec['spubid'];
    }
    $iauthor++;
    $apubs_final[$ipub]['authors'][$iauthor]['sauthor_first'] = $arec['sfirst'];
  } 
  </code></pre>
  
  <p>Thanks to everybody who responded, your help is/was much appreciated!</p>