Parsing a PHP Array

Hi everyone, I have a huge array returning as search results, and I want to do the following:

Go through the array and for each record with the same "spubid" add the following / vals switches: "sfirst, smi, slast" to the member of the parent array in this case, $ a [0]. Thus, the result will leave $ a [0] in tact, but adds the values ​​from sfirst, smi and slast from other members of the array to it (since they all have the same "spubid"). I think adding the key value (1, 2, 3) to the associated key (sfirst1 => "J.", smi1 => "F.", Slast1 => "Kennedy") will be fine. Then I would like to DROP (unset ()) the remaining members of the array with this "spubid". Here is a simplified example of an array that I am returning, and in this example all entries have the same "spubid":

Array ( 
  [0] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => A. 
      [slast] => Doe
  [1] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => F. 
      [slast] => Kennedy 
  [2] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => B. 
      [smi] => F. 
      [slast] => James 
  [3] => 
    Array ( 
      [spubid] => A00502
      [sfirst] => S. 
      [smi] => M. 
      [slast] => Williamson 
      )
    )

, KEEP $a [0], = > (sfirst $key, smi $key, slast $key) "sfirst, smi, slast" "" $a [1] - [3].

, IDEAL:

Array ( 
  [0] => 
    Array ( 
      [spubid] => A00502 
      [sfirst] => J. 
      [smi] => A. 
      [slast] => Doe
      [sfirst1] => J.
      [smi1] => F. 
      [slast1] => Kennedy
      [sfirst2] => B. 
      [smi2] => F. 
      [slast2] => James 
      [sfirst3] => S. 
      [smi3] => M. 
      [slast3] => Williamson
    )
  )

, , "spubid" , 99% , .

*** UPDATE

, , , , , . Chacha102 zombat, " " A LOT , , . , :

[spubid] => A00680 
[bactive] => t 
[bbatch_import] => t 
[bincomplete] => t 
[scitation_vis] => I,X 
[dentered] => 2009-08-03 12:34:14.82103 
[sentered_by] => pubs_batchadd.php 
[drev] => 2009-08-03 12:34:14.82103 
[srev_by] => pubs_batchadd.php 
[bpeer_reviewed] => t 
[sarticle] => A case study of bora-driven flow and density changes on the Adriatic shelf (January 1987)
.
.
.
.
.

40 , . , , , , , . ( ) , , , .

**** :

, . , .

:

$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'];
} 

, , / !

+3
3
// First, probably the more parsable way.
foreach($array as $key => $values)
{
    $end[$spuid] = $values;
    $spuid = $values['spuid']
    $end[$spuid]['authors'][] = array('sfirst' => $values['sfirst'],
                          'smi' => $values['smi'],
                           'slast' => $values['slast']);

}

,

Array(
    [A00502] =>
         Array(
           [supid] => A00502
               .... other values .....
           [authors] =>
                 Array(
                [0]=>
                      Array(
                    ['sfirst'] => '',
                    ['smi'] => '',
                    ['slast'] => '')
                )
        )
)

, , , , , .

,

$count = 0;
foreach ($end as $supid => $values)
{
    $other_end[$count] = $values;
    $other_end[$count]['spuid'] = $spuid;
    foreach($values['authors'] as $key => $author)
    {
        if($key == 0)
        {
            $suffix = '';
        }
        else
        {
            $suffix = $key;
        }
        $other_end[$count]['sfirst'.$suffix] = $author['sfirst'];
        $other_end[$count]['smi'.$suffix] = $author['smi'];
        $other_end[$count]['slast'.$suffix] = $author['slast'];
    }

}
+3

spubid:

// assuming $array is your array:

$storage = array();
foreach($array as $entry) {
  $bid = $entry['spubid'];
  if (!isset($storage[$bid])) {
    // duplicate entry - taking the author out of it.
    $stortmp = $entry;
    unset($stortmp['sfirst'], $stortmp['smi'], $stortmp['slast']);
    // add an authors array
    $stortmp['authors'] = array();
    $storage[$bid] = $stortmp;
  }
  $author = array(
    'sfirst' => $entry['sfirst'], 
    'smi' => $entry['smi'], 
    'slast' => $entry['slast']);
  $storage[$bid]['authors'][] = $author;
}

$ :

Array(
  "A00502" => Array(
    "spubid" => "A00502",
    "authors" => Array(
      [0] => 
        Array ( 
          [sfirst] => J. 
          [smi] => A. 
          [slast] => Doe
       [1] => 
         Array ( 
          [sfirst] => J. 
          [smi] => F. 
          [slast] => Kennedy

, :

foreach ($storage as $pub) {
  echo 'Pub ID: '.$pub['spubid']."<br/>";
  foreach ($pub['authors'] as $author) {
    echo 'Author: '.$author['sfirst'].' '.$author['smi'].' '.$author['slast']."<br/>";
  }
}

$storage['A00502'].

, , , - SQL-, JOIN . , , . , / . , , , "", - :

SELECT * FROM authors WHERE spubid IN ('A00502', 'A00503', 'A00504');

. .

+2

This code should work exactly as you indicated. I took the opportunity to use a pair of temporary arrays to correlate between the main keys of the array and submaps spubid.

/* assume $array is the main array */
$array = array(
    array('spubid' => 'A00502','sfirst'=>'J.','smi'=>'A.','slast'=>'Doe'),
    array('spubid' => 'A00502','sfirst'=>'J.','smi'=>'F.','slast'=>'Kennedy'),
    array('spubid' => 'A00502','sfirst'=>'B.','smi'=>'F.','slast'=>'James'),
    array('spubid' => 'BXXXXX','sfirst'=>'B.','smi'=>'F.','slast'=>'James'),
    array('spubid' => 'A00502','sfirst'=>'S.','smi'=>'M.','slast'=>'Williamson')
);

//track spubid positions in the main array
$keyPositions = array();
//keys to delete after array iteration
$keyDel = array();
//track how many spubkey increments you've made to the fields
$spubKeys = array();
//fields to copy between spubids
$copyFields = array('sfirst','smi','slast');

foreach($array as $key => $subarr)
{
    if (isset($subarr['spubid'])) {
        if (isset($keyPositions[$subarr['spubid']])) {
            //spubid already exists at a main array key, do the copy
            $spubKey = ++$spubKeys[$subarr['spubid']];
            foreach($copyFields as $f) {
                $array[$keyPositions[$subarr['spubid']]][$f.$spubKey] = $subarr[$f];
            }
            $keyDel[] = $key;
        }
        else {
            //First time encountering this spubid, mark the position
            $keyPositions[$subarr['spubid']] = $key;
            $spubKeys[$subarr['spubid']] = 0;
        }
    }
}
if (count($keyDel)) {
    foreach($keyDel as $idx) unset($array[$idx]);
}

var_dump($array);
+1
source

Source: https://habr.com/ru/post/1714265/


All Articles