我正在从数据库中选择值并将它们添加到循环中的数组中foreach,这是有效的。
之后,我在第二个查询中选择其他值,我想将这些值添加到同一个数组中,如何实现这一点?
请注意,我还不担心代码的安全性。
我的(简化的)代码如下;
$users_arr = array();
// first query
$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);
}
// second query
$db->setQuery("SELECT status AS wb_status FROM anothertable ");
$wb = $db->loadObject();
$wb_status = $wb->wb_status;
// add to original array
$users_arr[] = array("wb_status" => $wb_status);
echo json_encode($users_arr);
exit();
这会产生;
[
{
"id": "981",
"name": "jo",
"username": "jo123",
"email": "jo@example.com"
},
{
"wb_status": "Complete"
}
]
我需要这种格式;
[
{
"id": "981",
"name": "jo",
"username": "jo123",
"email": "jo@example.com",
"wb_status": "Complete"
}
]
海绵宝宝撒