我试图获得以下内容
<li class="player">
<a class="playerinfo>
<span class="playernumber">1</span>
<span class="playername">Angel</span>
</a>
</li>
<li class="player">
<a class="playerinfo>
<span class="playernumber">2</span>
<span class="playername">Shamak</span>
</a>
</li>
<li class="player">
<a class="playerinfo>
<span class="playernumber">4</span>
<span class="playername">Koal</span>
</a>
</li>
我需要从2个不同的foreach获得球员数量和球员名称,但我无法找到解决方案是否真的可以在Foreach内部使用Foreach
这是我到目前为止所尝试的内容
$repeatable1 = get_post_meta( get_the_ID(), 'meta-box-text', true);
foreach($repeatable1['playersnum'] as $num) {
echo '<li class="player">';
echo '<a class="playerinfo">';
echo ' <span class="playernumber">'.$num.'</span>';
}
foreach($repeatable1['playersname'] as $name) {
echo '<span class="playername">'.$name.'</span>';
echo '</a></li>';
}
由于
答案 0 :(得分:4)
您当前的循环不是嵌套的。但是,您可以使用index
一次打印所需的所有数据。索引将采用值0,1,2,3 ...
<?php foreach($repeatable1['playersname'] as $index=> $value):?>
<li class="player">
<a class="playerinfo>
<span class="playernumber"><?php echo $repeatable1['playersnum'][$index]; ?></span>
<span class="playername"><?php echo $value; ?></span>
</a>
</li>
<?php endforeach;?>
然而:理想情况下,您将重构$repeatable
数组,使其具有'player'
属性,该属性将包含sum和name的条目。
$repeatable = [
"player" => [
["name"=>"The Name","sum"=>100],
["name"=>"The other name","sum"=>200],
...
]
];