Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested foreach()

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";
}
like image 200
niczak Avatar asked Aug 10 '09 22:08

niczak


People also ask

What is nested foreach?

Nesting ForEach Tags consists of creating two loops, an inner loop and an outer loop, such that for each item returned by the outer loop, the inner loop executes completely and returns its items associated to the outer loop's item.

Can foreach be nested in JS?

An important feature of foreach is the %:% operator. I call this the nesting operator because it is used to create nested foreach loops. Like the %do% and %dopar% operators, it is a binary operator, but it operates on two foreach objects.

Can we use nested for each loop in Java?

Unfortunately, you cannot use it everywhere. Consider, for example, the expurgate method. The program needs access to the iterator in order to remove the current element. The for-each loop hides the iterator, so you cannot call remove.

Can I use for loop inside foreach?

Your for() isn't needed since foreach() already create a loop, you just have to use this loop to increment a value (here called $i) then display it. Also you should avoid to open your php tags ten thousands times for a better visibility into your code :) parsing HTML through PHP is an overhead.


1 Answers

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";
}
like image 162
mike Avatar answered Oct 08 '22 04:10

mike