毫无疑问,此前我还没有被问过,但我无法为我的生活找到答案。
我有一个看似简单的数组:
Array
(
[title] => Array
(
[0] => Price
[1] => BBB Rating
)
[description] => Array
(
[0] => How much you pay
[1] => Their standing with the BBB
)
)
我试图foreach
循环标题+每个键的描述,但由于某种原因,我所做的一切都无法正常工作。
预期输出为:
<ul>
<li>Price: How much you pay</li>
<li>BBB Rating: Their standing with the BBB</li>
</ul>
我试过了:
<?php foreach ($options[$overview] as $item => $value) : ?>
<li><?php echo $item[title][0] . ': ' . $item[description][0]; ?></li>
<?php endforeach; ?>
以及交换$ value,$ item,[0]等的每个变体
答案 0 :(得分:-1)
您可以使用常规的旧for
循环非常简单。您需要循环其中一个内部数组title / description的长度(假设它们是相同的)然后回显循环所在的索引的内容。
另外,在这样的实例中,我更喜欢字符串插值而不是连接。
$main = array(); // your array with price/ description
echo "<ul>";
for($i=0; $i <= count($main['title']); $i++){
echo "<li>";
echo "{$main['title'][$i]}: {$main['description'][$i]}";
echo "</li>";
}
echo "</ul>";
答案 1 :(得分:-1)
$entities = Array
(
[title] => Array
(
[0] => Price
[1] => BBB Rating
)
[description] => Array
(
[0] => How much you pay
[1] => Their standing with the BBB
)
)
所以可以这样实现。
<ul>
foreach($entities as $entity){
foreach($entity['title'] as $key => $title):
<li><?php echo $title[$key]; ?>: <?php echo $entity['description'][$key]; ?></li>
<?php endforeach ?>
}
</ul>