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

  • Home
  • SEARCH
  • 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 6977435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:37:32+00:00 2026-05-27T17:37:32+00:00

I have 2 arrays that look like this: $array1 = [ [‘id’ => ‘434b5g6’,

  • 0

I have 2 arrays that look like this:

$array1 = [
    ['id' => '434b5g6', 'unique_id' => 'banana', 'level' => 8],
    ['id' => 'bfrfnr', 'unique_id' => 'apple', 'level' => 4],
    ['id' => 'yt347509', 'unique_id' => 'grapefruit', 'level' => 9],
    ['id' => '456645', 'unique_id' => 'strawberry', 'level' => 1],
];

$array2 = [
    ['id' => 'gon235g6', 'unique_id' => 'strawberry', 'level' => 8],
    ['id' => 'bfrfnr', 'unique_id' => 'apple', 'level' => 4],
    ['id' => 'logujtng9', 'unique_id' => 'grapefruit', 'level' => 6],
    ['id' => '07yburhg', 'unique_id' => 'pinapple', 'level' => 1],
];

I need a way to remove the rows containing the same unique_id value in both arrays so that I’m left with 2 arrays which only contain elements that do not exist in the other.

Desired results as two separate arrays:

[['id' => '434b5g6', 'unique_id' => 'banana', 'level' => 8]]

and

[['id' => '07yburhg', 'unique_id' => 'pinapple', 'level' => 1]]

I know there is array_diff(), but this only works for single level arrays. I’m using multi-level arrays and only targeting the unique_id column for comparisons.

  • 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-27T17:37:33+00:00Added an answer on May 27, 2026 at 5:37 pm
    1. Get the unique ids out of both arrays.
    2. Calculate their difference (items that are not in both).
    3. Filter the original arrays to items that are in the diff.

    Here a sample for PHP 5.3+:

    $uniqueIds1 = array_map(function ($item) { return $item['unique_id']; }, $array1);
    $uniqueIds2 = array_map(function ($item) { return $item['unique_id']; }, $array2);
    
    $reallyUniqueIds = array_merge(array_diff($uniqueIds1, $uniqueIds2), array_diff($uniqueIds2, $uniqueIds1));
    
    $filteredArray1 = array_filter($array1, function ($item) use ($reallyUniqueIds) {
        return in_array($item['unique_id'], $reallyUniqueIds);
    });
    $filteredArray2 = array_filter($array2, function ($item) use ($reallyUniqueIds) {
        return in_array($item['unique_id'], $reallyUniqueIds);
    });
    

    Explanation:

    array_map(function ($item) { return $item['unique_id']; }, $array1)
    

    This just extracts all unique_id values into an array like array('banana', 'apple', ...).

    array_merge(array_diff($uniqueIds1, $uniqueIds2), array_diff($uniqueIds2, $uniqueIds1));
    

    This creates the diffs between the arrays both ways and merges them into one array, like:

    array('banana', 'apple')
    array('strawberry', 'apple')
    -> array('banana', 'strawberry')
    

    See array_diff.

    Then finally, this goes through the original arrays again to filter out all elements whose unique_key is not in the array we created in the previous step:

    array_filter($array1, function ($item) use ($reallyUniqueIds) {
        return in_array($item['unique_id'], $reallyUniqueIds);
    })
    

    This just uses a custom callback function for array_filter, which tells it to filter items where in_array($item['unique_id'], $reallyUniqueIds) is false.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bunch of php arrays that look like this (please don't ask
I currently have two arrays that look like this: Swatches: Array ( [0] =>
I have 2 arrays in PHP that look something like this: $rows = array(11,12,14,14,11,13,12,11);
I have two arrays that both look like this: Array ( [0] => Array
Let's say I have two classes that look like this: public class ByteFilter {
I have 2 arrays which might look something like this: $a1 = array('c','b','a'); $a2
I have a hash of arrays that looks like this: { $key, [$val1, $val2]
I have two arrays that I would like to compare and ultimately wind up
I have 2 arrays that I would like to render in a template, one
I have several arrays of arrays or arrays of dicts that I would like

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.