1 回答

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊
在第一個(gè)查詢之前運(yùn)行第二個(gè)查詢并將元素添加到數(shù)組中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并向每個(gè)元素插入所需的數(shù)據(jù):
// ...
$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 關(guān)注
- 196 瀏覽
添加回答
舉報(bào)