Output multidimensional array into 3 columns div and ul li in php - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, June 7, 2021

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

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!



source https://stackoverflow.com/questions/67862496/output-multidimensional-array-into-3-columns-div-and-ul-li-in-php

No comments:

Post a Comment