1

I am selecting values from a database and adding these to an array in a foreach loop, this works.

After this I am selecting additional values in a second query, I want to add these values to the same array, how can I achieve this?

Please note I am not concerned about the security of the code just yet.

My (simplified) code is below;

$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();

This produces;

[
  {
    "id": "981",
    "name": "jo",
    "username": "jo123",
    "email": "[email protected]"
  },
  {
    "wb_status": "Complete"
  }
]

I need it in this format;

[
  {
    "id": "981",
    "name": "jo",
    "username": "jo123",
    "email": "[email protected]",
    "wb_status": "Complete"
  }
]
3
  • What have you tried to achieve that? Should all entries have the same status value? Commented Jun 24, 2020 at 10:55
  • I just demonstrated in my question what I have tried to achieve that. Please let me know if I have omitted something? Also, I am only retrieving one entry, that's why I would like to merge the array into one. Commented Jun 24, 2020 at 11:01
  • 1
    Why not simply join the required data in with sql? From the given code, assuming it's not a simplified version, it seems it could be reduced to echo json_encode($db->loadObjectList()); provided, you set the correct query with $db->setQuery("...");. Here's an example of how to write the query: SELECT T1.id, T1.name, T1.username, T1.email, T2.status AS wb_status FROM sometable T1, anothertable T2; Ref: db-fiddle.com/f/k546HWvV9RLxV21F6vUViQ/0 Commented Jun 24, 2020 at 11:21

1 Answer 1

1

Run second query before first and add element to array in 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();

Update: another approach is to iterate over $users_arr and insert required data to each element:

// ...
$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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this! Is there another way to achieve the same thing? I ask because I have a similar scenario however my second query contains the result of the first query, so it needs to remain the second query - if that makes sense?!
Updated my answer, see another approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.