PHP不能将字符串偏移量用作数组错误

时间:2011-03-18 02:41:28

标签: php arrays string offset

我正在尝试从Rembmer the Milk API中检索任务。我运行这段代码:

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

但是我收到了这个错误:
致命错误:不能在第66行的C:**中使用字符串偏移作为数组   (第66行是$ duedate = $ val ['task'] ['due'];)

这是我试图解码的JSON响应(试图得到“名字”:“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":""}}}}}}

如何解决? 感谢!!!!!

更新 这是两个或多个任务的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 个答案:

答案 0 :(得分:1)

试试这个:

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