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'}; 

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.

Sign up to request clarification or add additional context in comments.

Comments

2

It is very easy:

$Arr = json_decode($JSON, true);

Comments

1

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

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

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

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

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.