1

I'm trying to retrieve tasks from Rembmer the Milk API. I run this code:

$array = json_decode($content, true);

foreach($array['rsp']['tasks']['list']['taskseries'] as $keys=>$val) {
$task = $val['name'];
$duedate = $val['task']['due'];
echo $task." ";
echo $duedate."<br>";
}

but I am getting this error:
Fatal error: Cannot use string offset as an array in C:** on line 66
(line 66 being $duedate = $val['task']['due'];)

This is the JSON response I am trying to decode (trying to get "name":"SOMETHING" and "due":"2011-03-17T04:00:00Z":

{"rsp":{"stat":"ok","tasks":{"rev":"[CODE]","list":{"id":"[ID NUMBER]","taskseries": {"id":"ID","created":"DATE CREATED","modified":"DATE","name":"SOMETHING","source":"js","url":"","location_id":"","tags":[],"participants":[],"notes":[],"task":{"id":"ID","due":"2011-03-17T04:00:00Z","has_due_time":"0","added":"DATE","completed":"","deleted":"","priority":"1","postponed":"0","estimate":""}}}}}}

How to fix? Thanks!!!!!

UPDATE This is the JSON response for two or more tasks:

{"rsp":{"stat":"ok","tasks":{"rev":"NUMBER","list":{"id":"ID NUMBER","taskseries":[{"id":"ID NUMBER","created":"CREATED DATE","modified":"DATE","name":"TASK 3","source":"js","url":"","location_id":"","tags":[],"participants":[],"notes":[],"task":{"id":"ID","due":"2011-03-18T04:00:00Z","has_due_time":"0","added":"DATE","completed":"","deleted":"","priority":"1","postponed":"0","estimate":""}},{"id":"ID","created":"DATE","modified":"DATE","name":"SOMETHING","source":"js","url":"","location_id":"","tags":[],"participants":[],"notes":[],"task":{"id":"ID","due":"2011-03-17T04:00:00Z","has_due_time":"0","added":"DATE","completed":"","deleted":"","priority":"1","postponed":"0","estimate":""}}]}}}}
0

1 Answer 1

1

Try this:

$taskSeries=$array['rsp']['tasks']['list']['taskseries'];
if(array_key_exists('id', $taskSeries)) {
    $taskSeries=array($taskSeries);
}
foreach($taskSeries as $task) {
    $name=$task['name'];
    $due=$task['task']['due'];
    // do something with $name and $due here
}
4
  • the API CAN return multiple tasks within the same json code, just not in the code shown... which is why i need the foreach loop... Commented Mar 18, 2011 at 3:11
  • @01jayss: Can you please update the question showing an example response with multiple tasks? Commented Mar 18, 2011 at 3:13
  • i tried running your code, but this error comes up: Notice: Undefined index: name in C:\** on line 67 and Notice: Undefined index: task in C:\** on line 68 (line 67:$name=$task['name']; line 68: $due=$task['task']['due'];... thanks! Commented Mar 18, 2011 at 15:32
  • @01jayss: Whoops, I had accidentally inserted an exclamation point where I didn't mean to. Works now. Commented Mar 18, 2011 at 19:35

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.