我有以下幾點multidimensional array:
$pages = array(
array(
"icon" => "",
"subheader" => "Insights",
"url" => "/insights/",
),
array(
"icon" => "",
"subheader" => "Statistics",
"url" => "/statistics/",
),
);
我正在嘗試使用上述內容回圈array并創建卡片。這是我回圈的方式array:
<?php
$keys = array_keys($pages);
for($i = 0; $i < count($pages); $i ) {
foreach($pages[$keys[$i]] as $key => $value) { ?>
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
</div>
<div class="productCard__body">
<!--subheader here -->
<?php echo $value; ?>
</div>
</div>
<?php }
}
?>
上面的回圈呈現出來(對于陣列中的一項):
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
</div>
<div class="productCard__body">
<!--subheader here -->
Insights
</div>
</div>
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
</div>
<div class="productCard__body">
<!--subheader here -->
/insights/
</div>
</div>
如您所見,它productCard為每個鍵生成一個單獨的鍵。
我希望實作的輸出是:
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
/insights/
</div>
<div class="productCard__body">
<!--subheader here -->
Insights
</div>
</div>
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
/statistics/
</div>
<div class="productCard__body">
<!--subheader here -->
Statistics
</div>
</div>
我哪里錯了?
uj5u.com熱心網友回復:
您正在遍歷陣列的每個元素的內部資料
for() 回圈遍歷主陣列,然后您的 foreach() 遍歷各個組件。
由于您的內部陣列是一個關聯陣列(鍵由您定義),這意味著您可以使用 [''] 符號直接訪問它們。
有很多不同的方法可以做到這一點,但我建議一個回圈,然后拉出你的個人價值觀:
嘗試這個:
<?php
foreach($pages as $key => $value) { ?>
<div class="productCard">
<div class="productCard__header">
<!-- url here-->
<?php echo $value['url']; ?>
</div>
<div class="productCard__body">
<!--subheader here -->
<?php echo $value['subheader']; ?>
</div>
</div>
<?php
}
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/340816.html
下一篇:對3Dnumpy陣列的第一維求和
