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": "jo@example.com"
  },
  {
    "wb_status": "Complete"
  }
]

I need it in this format;

[
  {
    "id": "981",
    "name": "jo",
    "username": "jo123",
    "email": "jo@example.com",
    "wb_status": "Complete"
  }
]
jonboy
  • 2,729
  • 6
  • 37
  • 77
  • What have you tried to achieve that? Should all entries have the same status value? – Nico Haase Jun 24 '20 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. – jonboy Jun 24 '20 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: https://www.db-fiddle.com/f/k546HWvV9RLxV21F6vUViQ/0 – Yoshi Jun 24 '20 at 11:21

1 Answers1

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();
u_mulder
  • 54,101
  • 5
  • 48
  • 64
  • 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?! – jonboy Jun 24 '20 at 10:58
  • Updated my answer, see another approach. – u_mulder Jun 24 '20 at 11:01