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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:53:28+00:00 2026-06-14T21:53:28+00:00

I have two arrays, they both look like array( array(‘key_id’ => 1, ‘value’ =>

  • 0

I have two arrays, they both look like

array(
  array('key_id' => 1, 'value' => 4, 'whatever' => 'something'),
  array('key_id' => 2, 'value' => 3, 'whatever' => 'something'),
  array('key_id' => 3, 'value' => 2, 'whatever' => 'something'),
  array('key_id' => 4, 'value' => 1, 'whatever' => 'something'),
);

They will have some intersecting key_id‘s, there will be some that are present in the first array, but not the second, and some that are present in the second but not the first.

I want to get an array of all key_id’s that are in the first array, but not the second. E.g.

$a = array(
  array('key_id' => 1, 'value' => 4, 'whatever' => 'something'),
  array('key_id' => 2, 'value' => 3, 'whatever' => 'something'),
  array('key_id' => 3, 'value' => 2, 'whatever' => 'something'),
);

$b = array(
  array('key_id' => 3, 'value' => 2, 'whatever' => 'something'),
  array('key_id' => 4, 'value' => 1, 'whatever' => 'something'),
);

The output I would want is array(1,2);

My current method is:

$keys = array('orig' => array(), 'new' => array());

array_map(function($array){
  $keys['orig'][] = $array['key_id'];
}, $a);

array_map(function ($array) {
  $keys['new'][] = $array['key_id'];
}, $b);

$difference = array_diff($keys['orig'], $keys['new']);

Which should work, but I am wondering if there is a better method?


Using eis‘s method below here is an working example of what I am doing to give context to the question:

<?php
$a = array(
  array('key_id' => 1, 'value' => 4, 'whatever' => 'something'),
  array('key_id' => 2, 'value' => 3, 'whatever' => 'something'),
  array('key_id' => 3, 'value' => 'o', 'whatever' => 'something'),
  array('key_id' => 5, 'value' => 'e', 'whatever' => 'something')
);

$b = array(
  array('key_id' => 3, 'value' => 'n', 'whatever' => 'something'),
  array('key_id' => 4, 'value' => 1, 'whatever' => 'something'),
  array('key_id' => 5, 'value' => 'e', 'whatever' => 'something')
));

function compareKeyId($a, $b) {
  if ($a['key_id'] === $b['key_id']) return 0;
  return ($a['key_id'] > $b['key_id']) ? 1 : -1;
}

function compareKeyIdAndValue($a, $b) {
    return ($a['key_id'] === $b['key_id'] && $a['value'] === $b['value']) ? 0 : -1;
  }

$changed = array();

$removed = array_udiff($a, $b, 'compareKeyId');
$added = array_udiff($b, $a, 'compareKeyId');

//get the elements that are present in both arrays (remainder)
$r = array();
$r['new'] = array_udiff($b, $removed, $added, 'compareKeyId');
$r['old'] = array_udiff($a, $removed, $added, 'compareKeyId');

$changed = array();
$changed['old'] = array_udiff($r['old'], $r['new'], 'compareKeyIdAndValue');
$changed['new'] = array_udiff($r['new'], $r['old'], 'compareKeyIdAndValue');

die('<pre>'.print_r(array('removed' => $removed, 'added' => $added, 'changed' => $changed), true).'</pre>');
?>
  • 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-14T21:53:29+00:00Added an answer on June 14, 2026 at 9:53 pm

    How about

    // get the elements
    function compare_col_key_id($a, $b) {
        if ($a['key_id'] === $b['key_id']) return 0;
        return ($a['key_id'] > $b['key_id']) ? 1 : -1;
    }
    $difference_elems = array_udiff($a, $b, 'compare_col_key_id');
    
    // and if you just want the keys out
    function get_col_key_id($a) {
        return $a['key_id'];
    }
    $difference_justkeys = array_map('get_col_key_id', $difference_elems);
    

    What this does better is that it first filters the elements and then creates a key-only array only if needed and only for the already-filtered elements. Otherwise it’s essentially the same thing, perhaps in a bit more clean way.

    I would possibly wrap it in a class that would get column id in the constructor and have the functions above as methods, using the column id from a private variable.

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

Sidebar

Related Questions

I have two arrays in JSON something like: proj1 proj1 taska taska charge1 charge1
I have two arrays... $Name = array(a, b, c, d); $Value = array(1, 2,
I have two arrays, N and M. they are both arbitrarily sized, though N
I have two string arrays Array1[size] and Array2[size]. They both have the same size.
There are two arrays with a ton of data in them. They both have
I have this two arrays, which come both from some checkbox selections and they
I have two 2D arrays. They both contain id and other, not so-related, stuff.
I have two arrays in php, both have a date value with 7 days.
Quick brief of what I'm doing: I have two arrays, they both contain 50%
I have two Ruby arrays, and I need to see if they have any

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.