1 回答
TA贡献1809条经验 获得超8个赞
在第一个查询之前运行第二个查询并将元素添加到数组中foreach:
// second query becomes first
$db->setQuery("SELECT status AS wb_status FROM anothertable ");
$wb = $db->loadObject();
$wb_status = $wb->wb_status;
$users_arr = array();
// first query becomes second
$db->setQuery("SELECT id, name, username, email FROM sometable WHERE name = '$name' ");
$results = $db->loadObjectList();
foreach ($results as $result) {
$userid = $result->id;
$name = $result->name;
$username = $result->username;
$email= $result->email;
$users_arr[] = array(
"id" => $userid,
"name" => $name,
"username" => $username,
"email" => $email
"wb_status" => $wb_status, // Here
);
}
echo json_encode($users_arr);
exit();
更新:另一种方法是迭代$users_arr并向每个元素插入所需的数据:
// ...
$db->setQuery("SELECT status AS wb_status FROM anothertable ");
$wb = $db->loadObject();
$wb_status = $wb->wb_status;
// I use `&` here so as to pass `$item` as a
// reference to original item in `$users_arr`
foreach ($users_arr as &$item) {
$item["wb_status"] = $wb_status;
}
echo json_encode($users_arr);
exit();
- 1 回答
- 0 关注
- 142 浏览
添加回答
举报