Reputation: 588
I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
Upvotes: 1
Views: 5238
Reputation: 1963
As everyone said, and you did - use json_decode
.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var)
and it will print type of variable and its content.
Upvotes: 2
Reputation: 34013
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings
Upvotes: 0
Reputation: 798626
json_decode()
will give you nested PHP types you can then descend to retrieve your data.
Upvotes: 1