amit
amit

Reputation: 1209

foreach loop not working correctly with json encode Php

I have multiple array (object inside array),But whnever i run foreach loop,its working one time only,What is the problem ? where i am wrong ?

Array
(
    [0] => stdClass Object
        (
            [_id] => 5cc6896028497b75f44cbf31
        }
    [1] => stdClass Object
        (
            [_id] => 5cc6896028497b75f44cbf32
        }
    ... 
}       

Here is my code,But loop is working only time but i have many records(more than 100)

<?php
$final = json_decode($response);
$record=$final->data;
foreach($record as $re)
        {
            echo $re->_id;
        }
?>      

Upvotes: 1

Views: 296

Answers (1)

fmsthird
fmsthird

Reputation: 1875

You can rewrite your foreach loop:

<?php

$final = json_decode($response,true);

foreach($final as $key => $re)
   {
     echo $re['_id']. "<br>";
   }

Upvotes: 1

Related Questions