How to loop a PHP array

0

Hello, I have this array that is inside another array and I would like to read it, but I will never know how many keys the array items will have, since it can always be increased or decreased, but what I was trying to do was

     foreach ($data->items as $row)
        {
            foreach($row as $i => $a)
            {
                echo '<div>'. $i." ".$a .'</div>';
            }
        }

But I do not know how to handle the increment of $ a

stdClass Object
 (
    [date_layout] => day-month-year
    [lastupdate] => 1545177599
    [date] => 19-12-18
    [rows] => 9
    [vbucks] => https://fortnite-public-files.theapinetwork.com/fortnite-vbucks-icon.png
    [items] => Array
        (
            [0] => stdClass Object
                (
                    [itemid] => 890fed5-625a443-274afd6-82af64c
                    [name] => Candy Axe
                    [cost] => 1500
                    [featured] => 1
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/pickaxe/4c6719bad5f8963492f5bbea0aa71f5d.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/pickaxe/4c6719bad5f8963492f5bbea0aa71f5d.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/890fed5-625a443-274afd6-82af64c.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => https://fortnite-public-files.theapinetwork.com/featured/890fed5-625a443-274afd6-82af64c.png
                                        )

                                )

                            [captial] => pickaxe
                            [type] => pickaxe
                            [rarity] => epic
                            [obtained_type] => vbucks
                        )

                )

            [1] => stdClass Object
                (
                    [itemid] => 2cae081-828dd4d-eb19098-83b342f
                    [name] => Red-nosed Ranger
                    [cost] => 800
                    [featured] => 1
                    [refundable] => 1
                    [lastupdate] => 1545204119
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/outfit/90fc29511f1dfe7b9a89143466666dda.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/outfit/90fc29511f1dfe7b9a89143466666dda.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/2cae081-828dd4d-eb19098-83b342f.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => https://fortnite-public-files.theapinetwork.com/featured/2cae081-828dd4d-eb19098-83b342f.png
                                        )

                                )

                            [captial] => outfit
                            [type] => outfit
                            [rarity] => uncommon
                            [obtained_type] => vbucks
                        )

                )

            [2] => stdClass Object
                (
                    [itemid] => eb15c77-d0ac8ec-ca444b0-c61443d
                    [name] => Red-Nosed Raider
                    [cost] => 1200
                    [featured] => 1
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/outfit/6d2d47c11279d5584da65d66acf74d77.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/outfit/6d2d47c11279d5584da65d66acf74d77.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/eb15c77-d0ac8ec-ca444b0-c61443d.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => https://fortnite-public-files.theapinetwork.com/featured/eb15c77-d0ac8ec-ca444b0-c61443d.png
                                        )

                                )

                            [captial] => outfit
                            [type] => outfit
                            [rarity] => rare
                            [obtained_type] => vbucks
                        )

                )

            [3] => stdClass Object
                (
                    [itemid] => 8caf27b-8e2e7c7-f2db92c-bed70f4
                    [name] => Candy Cane
                    [cost] => 300
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/wrap/db0904c6a420021907ff8b33cc69d158.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/wrap/db0904c6a420021907ff8b33cc69d158.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/8caf27b-8e2e7c7-f2db92c-bed70f4.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => 
                                        )

                                )

                            [captial] => wrap
                            [type] => wrap
                            [rarity] => uncommon
                            [obtained_type] => vbucks
                        )

                )

            [4] => stdClass Object
                (
                    [itemid] => a25b2dc-bb25249-fb0c8b5-0426bfa
                    [name] => Brilliant Striker
                    [cost] => 1200
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/outfit/04b4c416fd431a85e382466445952421.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/outfit/04b4c416fd431a85e382466445952421.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/a25b2dc-bb25249-fb0c8b5-0426bfa.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => 
                                        )

                                )

                            [captial] => outfit
                            [type] => outfit
                            [rarity] => rare
                            [obtained_type] => vbucks
                        )

                )

            [5] => stdClass Object
                (
                    [itemid] => 0e6f7ba-243f46e-494e1aa-f246fd7
                    [name] => Cliffhanger
                    [cost] => 800
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/pickaxe/49ddea37c8c827f96c8e7c5f24b3fafd.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/pickaxe/49ddea37c8c827f96c8e7c5f24b3fafd.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/0e6f7ba-243f46e-494e1aa-f246fd7.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => https://fortnite-public-files.theapinetwork.com/featured/0e6f7ba-243f46e-494e1aa-f246fd7.png
                                        )

                                )

                            [captial] => pickaxe
                            [type] => pickaxe
                            [rarity] => rare
                            [obtained_type] => vbucks
                        )

                )

            [6] => stdClass Object
                (
                    [itemid] => 7b20562-79552c5-e4e1ef1-a96612b
                    [name] => Shadow Ops
                    [cost] => 1500
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/outfit/532a2f3eb0ad9add755b7b88267cd73e.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/outfit/532a2f3eb0ad9add755b7b88267cd73e.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/7b20562-79552c5-e4e1ef1-a96612b.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => 
                                        )

                                )

                            [captial] => outfit
                            [type] => outfit
                            [rarity] => epic
                            [obtained_type] => vbucks
                        )

                )

            [7] => stdClass Object
                (
                    [itemid] => a1670bf-d97e209-dbb8870-e1c1cf3
                    [name] => Bring It
                    [cost] => 200
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/emote/5552c4be03f5fb3600948fc8647e2c1e.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/emote/5552c4be03f5fb3600948fc8647e2c1e.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/a1670bf-d97e209-dbb8870-e1c1cf3.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => 
                                        )

                                )

                            [captial] => emote
                            [type] => emote
                            [rarity] => uncommon
                            [obtained_type] => vbucks
                        )

                )

            [8] => stdClass Object
                (
                    [itemid] => 3d0cd9a-6467e15-2c82a41-b8f53d2
                    [name] => Hula
                    [cost] => 800
                    [featured] => 0
                    [refundable] => 1
                    [lastupdate] => 1545177599
                    [item] => stdClass Object
                        (
                            [image] => https://fortnite-public-files.theapinetwork.com/emote/d1fd812d3cfbd49f0918275bb75ad09c.png
                            [images] => stdClass Object
                                (
                                    [transparent] => https://fortnite-public-files.theapinetwork.com/emote/d1fd812d3cfbd49f0918275bb75ad09c.png
                                    [background] => https://fortnite-public-files.theapinetwork.com/image/3d0cd9a-6467e15-2c82a41-b8f53d2.png
                                    [featured] => stdClass Object
                                        (
                                            [transparent] => 
                                        )

                                )

                            [captial] => emote
                            [type] => emote
                            [rarity] => epic
                            [obtained_type] => vbucks
                        )

                )

        )

)
    
asked by anonymous 19.12.2018 / 12:27

1 answer

0

You should do this recursively.

Taking advantage of code for a recursive menu in html that I did before ( link ), and adjusting minimally to this case:

function recusive_list($items, $c) {
    $c += 1;
    $text = '';
    if (is_object($items))
        $items = get_object_vars($items);
    foreach ($items as $key => $item) {
        if(!is_array($item))
            $text .= str_repeat('*', $c) . ' ' .$key. ' - ' .$item. "\n";
        else
            $text .= recusive_list($item, $c);
    }
    return $text;
}

$items = [
    [
        'href' => 'www.item1',
        'name' => 'Item1',
        'childs' => [
            [
                'href' => 'www.item1.1',
                'name' => 'Item1.1',
                'childs' => []
            ]
        ]
    ],
    [
        'href' => 'www.item2',
        'name' => 'Item2',
        'childs' => [
            [
                'href' => 'www.item2.1',
                'name' => 'Item2.1',
                'childs' => [
                    [
                        'href' => 'www.item2.1.1',
                        'name' => 'Item2.1.1',
                        'childs' => []
                    ],
                    [
                        'href' => 'www.item2.1.2',
                        'name' => 'Item2.1.2',
                        'childs' => [
                            [
                                'href' => 'www.item2.1.2.1',
                                'name' => 'Item2.1.2.1',
                                'childs' => []
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ],
    [
        'href' => 'www.item3',
        'name' => 'Item3',
        'childs' => []
    ],
];

echo recusive_list($items, 0);

Output:

** href - www.item1
** name - Item1
**** href - www.item1.1
**** name - Item1.1
** href - www.item2
** name - Item2
**** href - www.item2.1
**** name - Item2.1
****** href - www.item2.1.1
****** name - Item2.1.1
****** href - www.item2.1.2
****** name - Item2.1.2
******** href - www.item2.1.2.1
******** name - Item2.1.2.1
** href - www.item3
** name - Item3

STATEMENT

PS: Note that you do not really need the variable $c (second parameter of the recusive_list function), just use it to make it easier to see:

    
19.12.2018 / 13:18