Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Patwan's avatar

Using a For loop on a PHP multi-dimensional array

Working on a multidimensional array in PHP (From an API) which is 4 levels deep. Am trying to use a for loop to separate/dissect the nested arrays so that they may exist as independent entities so that I may use them in the blade. e.g agency_sales , unit_sales, agents

When I use it in a foreach loop in the view I only get 1 array out of the possible 4 arrays....

They are stored in a variable called rsm

"regional_sales": [
    {
        "id": "75875",
        "agency_sales": [
            {
                "id": "157",
                "unit_sales": [
                    {
                        "id": "777",
                        "agents": [
                            {
                                "agent_no": "75939",
                                "policies": [
                                    "IL*********"
                                ]
                            },
                            {
                                "agent_no": "75939",
                                "policies": [
                                    "IL**********"
                                ]
                            }
                        ]
                    },
                    {
                        "id": "111",
                        "agents": [
                            {
                                "agent_no": "758",
                                "policies": [
                                    "IL2*********"
                                ]
                            },
                            {
                                "agent_no": "75939",
                                "policies": [
                                    "IL20**********"
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }
]

For loop am using

for($a=0; $a < count($rsm); $a++){
    $asm = $rsm[$a]['agency_sales'];
    //dd($asm);
    for($b = 0; $b < count($asm); $b++){
        $usm = $asm[$b]['unit_sales'];
        for($c = 0; $c < count($usm); $c++){
            $ag = $usm[$c]['agents'];
            //dd($ag);
        }   
    }
}

When I use it in a foreach loop I get only one array among the 4 arrays

 @foreach($usm as $u)
    <a href="#SubMenu1" class="list-group-item" data-toggle="collapse" data-parent="#SubMenu1"> USM ID : {{ $u['id'] }} <i class="fa fa-caret-down"></i></a>
 @endforeach
0 likes
3 replies
Snapey's avatar

When you DD you STOP on the first iteration.

try dump instead

Patwan's avatar

@SNAPEY - Duly noted Sir,, Tried your solution and it works,, why is it when I use the usm variable inside a foreach loop I get only one array but when I dump in the for loop I got 4 arrays..

Please check out the question.. I have edited it...

SteveCove's avatar

You are overwriting $usm (and the other variables) in each iteration of the loop, so you will always end up with the last value. If you want to collect all the values, you need to push to an array:

$usm = ...
$all_usm[]=$usm;

Please or to participate in this conversation.