PHP 添加到在 foreach 中创建的现有数组
PHP Adding to Existing Array Created in foreach
我从数据库中选择值并在 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"
}
]
运行 第二次查询 before 首先将元素添加到数组 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();
更新:另一种方法是遍历[=13=]并向每个元素插入所需的数据:
// ...
$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();
我从数据库中选择值并在 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"
}
]
运行 第二次查询 before 首先将元素添加到数组 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();
更新:另一种方法是遍历[=13=]并向每个元素插入所需的数据:
// ...
$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();