array row values are blank after the first row php

I am having a weird issue where after I loop through an array I can get all the first values from the first row, but after that row, all the values come in blank.

I have an array like so (there is much more I just shortened it)

Array
(
    [data] => Array
        (
            [0] => Array
                (
                    [id] => 76
                )
            [1] => Array
                (
                    [id] => 77
                )
            [2] => Array
                (
                    [id] => 78
                )
        )
)

In my php I loop through the array with

$result = json_decode(json_encode($result), true);
$i = 2;
for ($x = 0; $x <= $i; $x++) {
     // I do stuff here
}

If I loop through the array like below the first value comes in but the last 2 are blank, so I get (76)

$result = json_decode(json_encode($result), true);
$i = 2;
for ($x = 0; $x <= $i; $x++) {
     echo $result['data'][$x]['id'];
}

So I checked $x to make sure the values are correct (0,1,2) and they echo as 012

Here is where it gets weird. If I manually put in the number and echo I get the correct values for all three rows

echo $result['data'][0]['id'];
echo $result['data'][1]['id'];
echo $result['data'][2]['id'];

and from that, I get 767778

What the heck and I doing wrong?

Answer

Solution:

You shuld use "sizeof"

for($i = 0; $i < sizeof($result['data']) ;$i++){
    echo $result['data'][$i]['id'];
}

o foreach loop :

foreach($result['data'] as $data){
    echo $data['id'];
}

Answer

Solution:

Your loop should be:

$result = json_decode(json_encode($result), true);
$data = $result['data']??null;

for ($x = 0; $x < count($data); $x++) {
        echo $data[$x]['id'];
}

Source