从阵列获取响应

时间:2014-12-01 17:47:39

标签: php while-loop

我写道:

$app->get('/tasks', 'authenticate', function() {
            global $user_id;
            $response = array();
            $db = new DbHandler();

            // fetching all user tasks
            $result = $db->getAllUserTasks($user_id);

            $response["error"] = false;
            $response["tasks"] = array();

            // looping through result and preparing tasks array
            $response["error"] = false;
                $response["id"] = $result["id"];
                $response["task"] = $result["task"];
                $response["status"] = $result["status"];
                $response["createdAt"] = $result["created_at"];
                echoRespnse(200, $response);
        });

但是使用这段代码我只得到第一个结果[0] ......

{
error: false
tasks: [0]
id: 2
task: "Create something"
status: 0
createdAt: "2014-12-01 01:58:42"
}

如何在结果上循环以获取结果中的所有数据?

1 个答案:

答案 0 :(得分:1)

foreach($result as $row){
echo $row["id"];
 //-- and so on
}