2

I am running two queries, they return arrays similar to what is shown below:

First:

array(
    array(
        'id' => 1
    ),
    array(
        'id' => 2
    ),
    array(
        'id' => 3
    ),
    array(
        'id' => 4
    ),
)

Second:

array(
    array(
        'id' => 4
    ),
    array(
        'id' => 5
    ),
    array(
        'id' => 6
    ),
    array(
        'id' => 7
    ),
)

But I want to end up with

$ids = array(1,2,3,4,5,6,7);

But the only way I can think of to do that is

$ids = array();
foreach(array($array1, $array2) as $a){
    foreach($a as $id){
        $ids[] = $id['id'];
    }
}
$ids = array_unique($ids);

But that doesn't seem very efficient to me, and with the wealth of array functions out there, I am wondering if there is a better way?

7
  • Have you tried array_merge? Commented Feb 1, 2013 at 2:55
  • array_merge & stackoverflow.com/questions/1319903/… Commented Feb 1, 2013 at 2:58
  • are you sure you cant do this with a single querry Commented Feb 1, 2013 at 2:59
  • @Dagon here are the two queries: SELECT game_id FROM user_game WHERE user_id = :userid and SELECT id FROM games WHERE referee_id = :userid OR reviewer_id = :userid (syntax may be off as I use a query builder, not raw sql. but you get the idea) So, could I? Commented Feb 1, 2013 at 2:59
  • yes you could join games on user_id=referee_id Commented Feb 1, 2013 at 3:15

2 Answers 2

2

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

Comments

0

Not sure if this is what you are looking for but try:

array_unique(array_merge($firstArr, $secndArr));

Probably not as your arrays are associative but at least you can skip some of your steps.

1 Comment

This doesn't flatten the data.

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.