John Davidson

html - Output multidimensional array into 3 columns div and ul li in php

0 comments
Message:


Here's an array : what I am trying to split the array into 3 column of divs with first array should be wrapped into a h3 tag then a other arrays should be displayed in an unordered list.


Array
(
[0] => Array
(
[label_01] => heading 01:
[label_02] => heading 02:
[label_03] => heading 03:
)

[1] => Array
(
[label_01] => item 01
[label_02] => item 02
[label_03] => item 03
)

[2] => Array
(
[label_01] => item 001
[label_02] => item 002
[label_03] => item 003
)

)

It should output like this


<div class="row">
<div class="col-lg-4">
<h3 class="text-primary text-center">heading 01:</h3>
<ul class="list-box">
<li class="bg-primary text-white">item 01</li>
<li class="bg-primary text-white">item 001</li>
</ul>
</div>
<div class="col-lg-4">
<h3 class="text-warning text-center">heading 02:</h3>
<ul class="list-box">
<li class="bg-warning text-white">item 02</li>
<li class="bg-warning text-white">item 002</li>
</ul>
</div>
<div class="col-lg-4">
<h3 class="text-blue text-center">heading 03:</h3>
<ul class="list-box">
<li class="bg-blue text-white">item 03</li>
<li class="bg-blue text-white">item 003</li>
</ul>
</div>
</div>

I've tried so far


<div class="row">
<?php
$keys = array_keys($columns);
for($i = 0; $i < count($columns); $i++) { ?>
<div class="col-lg-4">
<?php foreach($columns[$keys[$i]] as $key => $value) { ?>
<li class="bg-primary text-white"><?php echo $value; ?></li>
<?php } ?>
</div>
<?php } ?>
</div>
</div>
<?php } ?>
</div>

and this


<div class="row">
<?php foreach ( $columns as $column ) { ?>
<div class="col-lg-4">
<h3 class="text-primary text-center">Inspire: Self Leadership</h3>
<ul class="list-box">
<?php foreach ($column as $val) { ?>
<li class="bg-primary text-white"><?php echo $val; ?></li>

<?php } ?>
</ul>
</div>
<?php } ?>
</div>

Any help will be greatly appreciated! Thank you in advance!



My Answer:
array("Apple", "Banana", "Cherry"),
[1] = array("Orange", "Grape", "Watermelon"),
[2] = array("Pineapple", "Strawberry", "Kiwi")
);

php
echo '
';

foreach ($array as $subarray) {
echo '
';

echo '

' . $subarray[0] . '

';

echo '
    ';
    foreach ($subarray as $item) {
    if ($item != $subarray[0]) {
    echo '
  • ' . $item . '
  • ';
    }
    }
    echo '
';

echo '
';
}

echo '
';

Rate this post

3 of 5 based on 5245 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us