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 as012
Here is where it gets weird. If I manually put in the number andecho
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 get767778
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'];
}
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: deprecated: directive 'allow_url_include' is deprecated in unknown on line 0
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.