I have the following multidimensional array:
$pages = array(
array(
"icon" => "",
"subheader" => "Insights",
"url" => "/insights/",
),
array(
"icon" => "",
"subheader" => "Statistics",
"url" => "/statistics/",
),
);
I'm trying to loop through the array and create cards with the above. Here's how I'm looping through the 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 }
}
?>
The above loop renders out (for one item in the array):
<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>
As you can see, it's generating a separate productCard for each key.
The output I'm looking to achieve is:
<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>
Where am I going wrong?
$pagesisn't an associative array, why are you usingarray_keys($pages)?