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'}; 
Vee Mandke
  • 578
  • 1
  • 11
  • 28

5 Answers5

2

It is very easy:

$Arr = json_decode($JSON, true);
Roman Newaza
  • 11,405
  • 11
  • 58
  • 89
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.

Zagor23
  • 1,953
  • 12
  • 14
1

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

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 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'}; – Vee Mandke Jul 17 '12 at 10:05
1

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

Krishna
  • 353
  • 2
  • 15
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,384
  • 21
  • 34
Gerald Versluis
  • 30,492
  • 6
  • 73
  • 100
  • 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'}; – Vee Mandke Jul 17 '12 at 10:04
  • 1
    @rekire It isn't actually in this case but I agree that's ugly http://stackoverflow.com/questions/5643496/are-php-functions-case-sensitive – allen213 Jul 17 '12 at 10:07
  • 2
    Try this: `$obj = json_decode($dateTimeArr); $dateAr = $obj->startDate;` You're mixing up object and array notation there – Dale Jul 17 '12 at 10:07