Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:31:07+00:00 2026-06-18T02:31:07+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:31:08+00:00Added an answer on June 18, 2026 at 2:31 am

    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.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am running two very similar update queries but for a reason unknown to
Assume that there are two queries running on a memory list; First query (employing
I am running two queries to my database for pagination reasons. As such, each
I have transactional replication running between two databases. I fear they have fallen slightly
I got two projects running, both written in PHP. Now I want to merge
I'm confused by the drastically different running times of the following two queries that
I have multiple ajax queries running at the same time, and I want them
Currently I'm running these two queries: SELECT COUNT(*) FROM `mytable` SELECT * FROM `mytable`
I'm running a MYSQL query in two steps. First, I get a list of
I am running the two following queries on a table where the refId table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.