There would be a few ways to handle this. I think I would probably start with array_merge()
on the two original arrays, then flatten it with array_map()
, and finally call array_unique()
.
// Combine them
$new = array_merge($array1, $array2);
// Flatten them
// array_map() is used to select the 'id' key from each
$new = array_map(function($a) {
return $a['id'];
}, $new);
// And get the unique values
$ids = array_unique($new);
print_r($ids);
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[5] => 5
[6] => 6
[7] => 7
)
Better: Do it in one query.
After seeing the query, these do not need to be two arrays. It can be done with a single UNION
query. Using a plain UNION
instead of a UNION ALL
will deduplicate them for you.
SELECT
game_id AS id
FROM user_game
WHERE user_id = :userid
UNION
SELECT
id
FROM games
WHERE referee_id = :userid OR reviewer_id = :userid
ORDER BY id
When fetching rows, since you have only one column, you may consider directly flattening it in the fetch.
// For example
while ($row = $stmt->fetch()) {
// Pluck only the id from the row fetched
$ids[] = $row['id'];
}
// $ids is now a 1D array.