我有一个网站,我正在使用 php 从数组中打印键和值
$array = array(
"gfo" => "Fondant",
"gdo" => "Domino",
"ges" => "Espir",
"gam" => "Amara",
"gsa" => "Sandwich",
"gme" => "Merme",
"cza" => "Zarza",
"cor" => "Oreo",
"cal" => "Almen",
"cca" => "eche",
"cch" => "Chocolate"
);
但我只想在菜单中显示前 6 个元素
<li>
<a href="product1.php" style="padding:8px 30px;">Product 1</a>
<ul>
<?php foreach($array as $key=>$val):?>
<li><a href="http://www.site.com.mx/products.php?id=<?=$key?>"><?=$val?></a></li>
<?php endforeach;?>
</ul>
</li>
然后是其他菜单中的其他 5 个元素
<li>
<a href="product2.php" style="padding:8px 30px;">Product 2</a>
<ul>
<?php foreach($array as $key=>$val):?>
<li><a href="http://www.site.com.mx/products.php?id=<?=$key?>"><?=$val?></a></li>
<?php endforeach;?>
</ul>
</li>
我知道我可以使用
for ($i = 1; $i <= 6; $i++) {
echo $i;
}
和
for ($i = 6; $i <= sizeof($array); $i++) {
echo $i;
}
但是如何在两个 for 循环中获取键和值呢?