1

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'}; 
asked Jul 17, 2012 at 9:57

5 Answers 5

2

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.

answered Jul 17, 2012 at 12:08

Comments

2

It is very easy:

$Arr = json_decode($JSON, true);
answered Jul 17, 2012 at 10:00

Comments

1

json_decode() will give you nested PHP types you can then descend to retrieve your data.

answered Jul 17, 2012 at 9:59

1 Comment

i want make array of start date, i have used these two lines but it wont work $obj, please suggest.. $obj = json_decode($dateTimeArr); $dateAr = $obj->{'startDate'};
1

use json_decode($json_response,true) to convert json to Array

answered Jul 17, 2012 at 10:04

Comments

0

Guess what you are looking for is json_decode()

Check out http://php.net/manual/en/function.json-decode.php for the inner workings

Dale
10.5k23 silver badges34 bronze badges
answered Jul 17, 2012 at 10:00

3 Comments

i want make array of start date, i have used these two lines but it wont work $obj, please suggest.. $obj = json_decode($dateTimeArr); $dateAr = $obj->{'startDate'};
@rekire It isn't actually in this case but I agree that's ugly stackoverflow.com/questions/5643496/…
Try this: $obj = json_decode($dateTimeArr); $dateAr = $obj->startDate; You're mixing up object and array notation there

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.