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 6537279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:35:53+00:00 2026-05-25T10:35:53+00:00

I try to combine result from several queries and put them into an array.

  • 0

I try to combine result from several queries and put them into an array. I found there are some duplications exist. What I want to do is keep the first record and remove the following same records if duplication exist. I want to write a function to filter array and remove duplications. Can anyone help me with it? thanks

array(4) {
  [0]=>
  array(14) {
    ["user_id"]=>
    string(7) "2620613"
    ["first_name"]=>
    string(5) "Susan"
    ["last_name"]=>
    string(9) "Alexander"
    ["client"]=>
    string(7) "Pro"
    ["ssn"]=>
    string(9) "4234324"
    ["hiredate"]=>
    string(10) "2008-04-11"
    ["hra_id"]=>
    string(6) "43244"
    ["hra_date"]=>
    string(10) "2011-08-17"
    ["hra_maileddate"]=>
    NULL
    ["screening_id"]=>
    string(6) "3764551"
    ["screening_date"]=>
    string(10) "2011-08-12"
    ["screening_maileddate"]=>
    NULL
    ["ref"]=>
    string(1) "D"
    ["mailable"]=>
    string(3) "Yes"
  }
  [1]=>
  array(14) {
    ["user_id"]=>
    string(7) "263453"
    ["first_name"]=>
    string(6) "Dharti"
    ["last_name"]=>
    string(6) "Surani"
    ["client"]=>
    string(7) "Pro"
    ["ssn"]=>
    string(9) "345325"
    ["hiredate"]=>
    string(10) "2002-04-29"
    ["hra_id"]=>
    string(6) "3455345"
    ["hra_date"]=>
    string(10) "2010-03-22"
    ["hra_maileddate"]=>
    NULL
    ["screening_id"]=>
    string(6) "234234"
    ["screening_date"]=>
    string(10) "2011-07-11"
    ["screening_maileddate"]=>
    NULL
    ["ref"]=>
    string(1) "D"
    ["mailable"]=>
    string(3) "Yes"
  }
  [2]=>
  array(14) {
    ["user_id"]=>
    string(7) "2685056"
    ["first_name"]=>
    string(4) "Alex"
    ["last_name"]=>
    string(2) "Hu"
    ["client"]=>
    string(7) "Pro"
    ["ssn"]=>
    string(9) "000005562"
    ["hiredate"]=>
    string(10) "2011-09-15"
    ["hra_id"]=>
    string(6) "528948"
    ["hra_date"]=>
    string(10) "2011-07-06"
    ["hra_maileddate"]=>
    NULL
    ["screening_id"]=>
    string(6) "377382"
    ["screening_date"]=>
    string(10) "2011-06-15"
    ["screening_maileddate"]=>
    NULL
    ["ref"]=>
    string(1) "D"
    ["mailable"]=>
    string(3) "Yes"
  }
  [3]=>
  array(14) {
    ["user_id"]=>
    string(7) "2685056"
    ["first_name"]=>
    string(4) "Alex"
    ["last_name"]=>
    string(2) "Hu"
    ["client"]=>
    string(7) "Pro"
    ["ssn"]=>
    string(9) "000005562"
    ["hiredate"]=>
    string(10) "2011-09-15"
    ["hra_id"]=>
    string(6) "528948"
    ["hra_date"]=>
    string(10) "2011-07-06"
    ["hra_maileddate"]=>
    NULL
    ["screening_id"]=>
    string(6) "377971"
    ["screening_date"]=>
    string(10) "2011-09-13"
    ["screening_maileddate"]=>
    NULL
    ["ref"]=>
    string(1) "E"
    ["mailable"]=>
    string(3) "Yes"
  }
}

I try to use the following function to remove duplications, but it doesn’t work very well and may cause error on some cases.

 $resultDE = array_merge($resultD1, $resultE1, $resultE2);
  function specified_array_unique($array) 
  { 
      for($i=0; $i<count($array); $i++){
        if(($array[$i]['user_id'] == $array[$i+1]['user_id']) && isset($array[$i+1]))
         unset($array[$i+1]); 
      }

      return $array; 
  } 

 var_dump(super_unique($resultDE));
  • 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-05-25T10:35:53+00:00Added an answer on May 25, 2026 at 10:35 am

    You need to decide which array key you are going to use to determine whether the record is a duplicate or not. In the following example, you would pass this key into the second parameter.

    EDIT I just made this MUCH more efficient..

    function array_remove_duplicates ($array, $key) {
      $temp = array();
      foreach ($array as $k => $record) {
        if (isset($record[$key])) {
          // We're only going to filter records that actually have the supplied key present
          if (in_array($record[$key],$temp)) {
            // Key already exists, ignore this one
            unset($array[$k]);
          } else {
            // Key doesn't exist, add it to the tracking array
            $temp[] = $record[$key];
          }
        }
      }
      // we have filtered out the records we don't want so re-order the indexes and return the array
      return array_merge($array);
    }
    
    // ...and you could use it like this
    $filteredArray = array_remove_duplicates($originalArray,'user_id');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some of my script are using different encoding, and when I try to combine
Is there any solution combine 2 table with different structure I try to use
I'm testing out the Reactive Extensions (main branch from NuGet) and I'm having some
I'm very new to Matlab and I'm looking for some advice from someone who
Using Firebird, I want to combine the results of two queries using UNION ALL,
I've read on SO (and from other websites found on Google after I tried
I have 2 queries which work fine: var q = (from c in _context.Wxlogs
I am playing with Parsec and I want to combine two parsers into one
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
try { ... } catch (SQLException sqle) { String theError = (sqle).getSQLState(); ... }

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.