简体   繁体   中英

PHP foreach multidimensional array

I have an multidimensional array like this:

Array
(
    [Category1] => Array
        (
            [1] => Array
                (
                    [0] => Img 1
                    [1] => Title 1
                    [2] => Text 1
                )

            [5] => Array
                (
                    [0] => Img 17
                    [1] => Title 17
                    [2] => Text 5
                )

            [10] => Array
                (
                    [0] => Img 1
                    [1] => Title 6
                    [2] => Text 10
                )

            [16] => Array
                (
                    [0] => Img 16
                    [1] => Title 12
                    [2] => Text 16
                )

        )

    [Category2] => Array
        (
            [2] => Array
                (
                    [0] => Img 2
                    [1] => Title 2
                    [2] => Text 2
                )

            [6] => Array
                (
                    [0] => Img 18
                    [1] => Title 18
                    [2] => Text 6
                )

            [13] => Array
                (
                    [0] => Img 13
                    [1] => Title 9
                    [2] => Text 13
                )

        )

    [Category3] => Array
        (
            [3] => Array
                (
                    [0] => Img 3
                    [1] => Title 3
                    [2] => Text 3
                )

            [7] => Array
                (
                    [0] => Img 19
                    [1] => Title 19
                    [2] => Text 7
                )

            [11] => Array
                (
                    [0] => Img 11
                    [1] => Title 7
                    [2] => Text 11
                )

            [14] => Array
                (
                    [0] => Img 14
                    [1] => Title 10
                    [2] => Text 14
                )

        )

    [Category4] => Array
        (
            [4] => Array
                (
                    [0] => Img 4
                    [1] => Title 4
                    [2] => Text 4
                )

            [8] => Array
                (
                    [0] => Img 20
                    [1] => Title 20
                    [2] => Text 8
                )

            [15] => Array
                (
                    [0] => Img 15
                    [1] => Title 11
                    [2] => Text 15
                )

        )

    [Category5] => Array
        (
            [9] => Array
                (
                    [0] => Img 9
                    [1] => Title 5
                    [2] => Text 9
                )

        )

    [Category6] => Array
        (
            [12] => Array
                (
                    [0] => Img 12
                    [1] => Title 8
                    [2] => Text 12
                )

        )

)

I need help to view like this, there $myarray is as on top example. In the firs foreach all it's OK but in the sencond and the third foreach if i have more then one array at one category it is dublicated and displayed not corectly.

<div id="tab-container">
        <ul>
            <?php foreach ($myarray as $category => $detail) { ?>
                <li><a href="#tab-<?=$category?>"><?=$category?></a></li>
            <?php }; ?>
        </ul>

    <div>
        <?php foreach (??? ??? ???) { ?>
            <div id="tab-<?=$category?> for ex Category 1 contain Array (1, 5 10, 16)">
                <div class="tab-content_left">
                    <div><img src="<?=base_url();?>img/$value[0] for ex Img 1" /></div>
                    <div>$value[1] for ex Title 1</div>
                    <div>$value[2] for ex Text 1</div>
                </div>

                <div class="tab-content_right">
                    <?php foreach (??? ??? ???) { ?>
                        <div><img src="<?=base_url();?>img/"$value[0] for ex Img 17 /></div>
                        <div>$value[1] for ex. Title 17</div>
                        <div>$value[2] for ex Text 5</div>
                    <?php }; ?>
                </div>
            </div>
        <?php }; ?>
    </div>
</div>

THX. Please some ideas how can i put foreach cicles and where?

First foreach:

<?php foreach ($mainArray as $categoryKey => $singleCategory) { ?>

Second foreach:

<?php foreach ($singleCategory as $categoryEntry) { ?>

Note:

To get the category name write $categoryKey in the first foreach. To get data write: $singleCategory['image'] inside the second foreach.

Check foreach documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM