3

I'm getting the following 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 startDate and totalTime, i have used these two lines but it wont work, please suggest..

    $obj  = json_decode($dateTimeArr); 
    $dateAr = $obj->{'startDate'}; 
1
  • A suggestion: How does it know which of the objects in the JSON response it should get the start date of? Try making sure that you're getting the first of the three, perhaps. Commented Jul 17, 2012 at 10:27

1 Answer 1

5

Your JSON string represents an Array or Objects. Each item of the array is an object like {"startDate":"2012-07-12 11:21:38 +0530","totalTime":0}.

So json_decode($dateTimeArr); returns the array. If you want to access to the first element you can use the $obj[0] syntax. Then to get the startDate property, use $obj[0]->startDate.

You can iterate over all array's items using foreach:

foreach ($obj as $item) {
    echo $item->startDate, "\n";
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.