php 嵌套 foreach()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1257519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:46:27  来源:igfitidea点击:

Nested foreach()

phparraysloopsforeach

提问by Nicholas Kreidberg

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?

我正在使用嵌套的 foreach() 遍历外部数组中的元素,但是在吐出作者列表时,我遇到了问题。即由于疯狂的 foreach() 嵌套而导致每个输出多次(多次)的问题。在这个例子中,有什么比嵌套 foreach() 循环更好的方法?

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";
}

采纳答案by michal kralik

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";
}

回答by mermshaus

Just for fun. If you reallywant 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";
});

回答by Jani Hartikainen

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.

当您拥有迄今为止处理过的所有作者的数组时,您可以轻松比较该作者是否已经在其中。

回答by Bostone