gpt4 book ai didi

php - 嵌套 foreach()

转载 作者:行者123 更新时间:2023-12-03 00:01:39 26 4
gpt4 key购买 nike

我有以下数组:

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 )
)
)
)

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

更新(含解决方案)

这是我确定的循环,有点困惑(恕我直言),但它有效:

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

最佳答案

你为什么不这样做

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

关于php - 嵌套 foreach(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1257519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com