PHP не может использовать смещение строки как массив Ошибка - PullRequest
1 голос
/ 18 марта 2011

Я пытаюсь получить задачи из API Rembmer the Milk. Я запускаю этот код:

$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>";
}

но я получаю эту ошибку:
Неустранимая ошибка: невозможно использовать смещение строки в качестве массива в C: ** в строке 66
(строка 66: $ duedate = $ val ['task'] ['due'];)

Это ответ JSON, который я пытаюсь декодировать (пытаясь получить "name": "SOMETHING" и "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":""}}}}}}

Как исправить? Спасибо !!!!!

UPDATE Это ответ JSON для двух или более задач:

{"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":""}}]}}}}

1 Ответ

1 голос
/ 18 марта 2011

Попробуйте это:

$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
}
...