Vidyadhar Mandke
Vidyadhar Mandke

Reputation: 588

How to parse JSON string in php

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

Answers (5)

Zagor23
Zagor23

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

Gerald Versluis
Gerald Versluis

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

Krishna
Krishna

Reputation: 353

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

Upvotes: 1

Roman Newaza
Roman Newaza

Reputation: 11690

It is very easy:

$Arr = json_decode($JSON, true);

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798626

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

Upvotes: 1

Related Questions