0

I need one help.I need to get the json decode value inside foreach loop using PHP. I am explaining my code below.

    $user_qry = "http://oditek.in/takeme/webservice/user2/user_ride_list_v2.php";
$pstflds="mobile=".$mobile;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $user_qry);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $pstflds);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response=curl_exec ($ch);
$data=json_decode($response,true);
$bikeArr=$data->data;
print_r($bikeArr);exit;

Its giving the output like below.

Array ( [0] => stdClass Object ( [book_id] => 161117193881 [booking_status] => 4 [book_date] => 17-11-2016 [book_time] => 07:38 PM ) [1] => stdClass Object ( [book_id] => 161116183564 [booking_status] => 4 [book_date] => 16-11-2016 [book_time] => 06:35 PM  ) )

I need to iterate the above array in foreach loop and get the data.Please help me.

  • Possible duplicate of [json\_decode to array](http://stackoverflow.com/questions/5164404/json-decode-to-array) – LF00 Feb 20 '17 at 09:59

3 Answers3

0

try

$data=json_decode($response,true);

and youll get assoc array instead of object

Michał G
  • 2,234
  • 19
  • 27
0

json_decode parameter 2 is assoc

When TRUE, returned objects will be converted into associative arrays.

default is false

reference http://php.net/manual/en/function.json-decode.php

json_decode($response, true);
Jin.C
  • 121
  • 5
0

Can you try this and see if you get any errors?

foreach($bokeArr as $bike){
 echo 'book id = ' . $bike->book_id;
}
rypskar
  • 2,012
  • 13
  • 13