1

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?

2
  • Just loop through the array twice. Commented Feb 1, 2013 at 14:17
  • I tray this but i confused with second and third foreach. Commented Feb 1, 2013 at 15:05

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In my first foreach $categoryKey = Category_name and $singleCategory = array
THX Vlad. In the first and second foreach i need the same $singleCategory value from array, it work ok but if i have more items as the same category i can't display right. OK THX Vlad i weel try from begining.
You have every item in the category in the second foreach. That's the purpose of using a second foreach. I'm not sure what you're trying to accomplish, but it seems like you want the same content to be shown twice, which is not very good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.