嵌套的foreach() [英] Nested foreach()

查看:112
本文介绍了嵌套的foreach()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组:

 阵列(
  [1] =>阵列(
    [spubid] => A00319
    [sentered_by] => pubs_batchadd.php
    [sarticle] =>奥里诺科河,Atabapo水域横向混合
    [spublication] => ACTA Cientifica Venezolana
    [雄鹿] => ACTA,合流,奥里诺科河,河流,委内瑞拉水域
    [作者] =>阵列(
      [1] =>阵列(
        [STYPE] =>作者
        [iorder] => 1
        [sfirst] =>一个
        [slast] =>安德森)
      [2] =>阵列(
        [STYPE] =>作者
        [iorder] => 2
        [sfirst] => S.
        [slast] =>约翰逊)
      [3] =>阵列(
        [STYPE] =>作者
        [iorder] => 3
        [sfirst] => J.
        [slast] => DOE)
      )
    )
  )

我使用一个嵌套的foreach()外数组中通过要素走,但是当涉及到​​随地吐痰作者的名单,我遇到了问题。即输出由于疯狂的foreach()嵌套中的每一个的多个(多)倍的问题。什么是比这个例子中嵌套的foreach()循环更好的方法?

UPDATE(带解决方案)

下面是循环我定居在,有点乱(恕我直言),但它的工作原理:

  $ sauthors = NULL;
$ stitle = NULL;的foreach($ apubs为$ apub)
{
  $ stitle = $ apub ['sarticle'];
  的foreach($ apub为$ SVAR => $ SVAL)
  {
    如果($ SVAR ===作家)
    {
      的foreach($ SVAL为apeople $)
      {
        $ sauthors = $ apeople ['slast'],$ apeople ['sfirst']。;;
      }
    }
  }
  回声$ sauthors< BR /> \\ n $的stitle< BR /> \\ N的;
}


解决方案

你为什么不这样做。

 的foreach($ apubs为$ apub){
  $ sauthors ='';
  $ stitle = $ apub ['sarticle'];
  的foreach($ apub ['作家']为$作家){
    $ sauthors = $作者['slast'],$作者['sfirst']。;;
  }  回声$ sauthors< BR /> \\ n $的stitle< BR /> \\ N的;
}

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

解决方案

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

这篇关于嵌套的foreach()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆