Nested foreach()

76,107

Solution 1

Why don't you do

foreach($apubs as $apub) {
  $sauthors = '';
  $stitle = $apub['sarticle'];
  foreach($apub['authors'] as $author) {
    $sauthors .= $author['slast'].", ".$author['sfirst']."; ";
  }

  echo "$sauthors<br />\n$stitle<br />\n";
}

Solution 2

Just for fun. If you really want to avoid loops, try this:

// Pre PHP 5.3:

function cb2($e)
{
    return $e['slast'] . ', ' . $e['sfirst'];
}

function cb1($e)
{
    $authors = array_map('cb2', $e['authors']);
    echo implode('; ', $authors) . ":<br />\n" . $e['sarticle'] . "<br />\n";
}

array_walk($data, 'cb1');



// PHP 5.3 (untested):

array_walk($data, function($e)
{
    $authors = array_map(function($e)
    {
        return $e['slast'] . ', ' . $e['sfirst'];
    },
    $e['authors']);

    echo implode('; ', $authors) . ":<br />\n" . $e['sarticle'] . "<br />\n";
});

Solution 3

If your problem is that you have the same author on multiple articles and thus getting output more than once, the simplest solution is to build an array of authors instead of outputting them right away.

When you have an array of all the authors you've processed so far you can easily compare if this author is already in there or not.

Solution 4

Take a look at this

Share:
76,107
niczak
Author by

niczak

Husband, Father, Learner, Wanderer. Loves: #Code #Data #DigitalArchitecture #Humankind #Music #Photography #Tech4Good

Updated on August 12, 2020

Comments

  • niczak
    niczak over 3 years

    I have the following array:

    Array ( 
      [1] => Array ( 
        [spubid] => A00319 
        [sentered_by] => pubs_batchadd.php
        [sarticle] => Lateral mixing of the waters of the Orinoco, Atabapo
        [spublication] => Acta Cientifica Venezolana
        [stags] => acta,confluence,orinoco,rivers,venezuela,waters
        [authors] => Array ( 
          [1] => Array ( 
            [stype] => Author 
            [iorder] => 1 
            [sfirst] => A
            [slast] => Andersen ) 
          [2] => Array ( 
            [stype] => Author 
            [iorder] => 2 
            [sfirst] => S.
            [slast] => Johnson ) 
          [3] => Array ( 
            [stype] => Author 
            [iorder] => 3 
            [sfirst] => J. 
            [slast] => Doe ) 
          ) 
        ) 
      )
    

    I am using a nested foreach() to walk through the elements in the outer array but when it comes to spitting out the list of authors I am running into problems. Namely the problem of outputting each one multiple (multiple) times because of the crazy foreach() nesting. What would be a better approach than nesting foreach() loops in this example?

    UPDATE (With solution)

    Here is the loop I settled on, a bit messy (IMHO) but it works:

    $sauthors = NULL;
    $stitle = NULL;
    
    foreach($apubs as $apub)
    {
      $stitle = $apub['sarticle'];
      foreach($apub as $svar=>$sval)
      {
        if($svar === "authors")
        {
          foreach($sval as $apeople)
          {
            $sauthors .= $apeople['slast'].", ".$apeople['sfirst']."; ";
          }
        }
      }
      echo "$sauthors<br />\n$stitle<br />\n";
    }
    
  • edorian
    edorian over 11 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • edorian
    edorian over 11 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.
  • Bostone
    Bostone over 11 years
    @edorian - I was referring to all answers in that Stackoverflow thread. If they ever break these links it would be most unfortunate, so I highly doubt it