1

I have two arrays that have identical keys. I want to check array a against array b and return the the whole row of the array of a that is NOT in b. I am messing with all of them and can't get the desired results. my arrays look like this:

//array a
Array
(
[0] => Array
    (
        [pid] => 457633
        [name] => Test
        [descr] => sample
        [creator] => 
        [datetime] => 
    )

) 
 //array b
 Array
  (
    [0] => Array
    (
        [pid] => 1234
        [name] => server
        [descr] => server
        [creator] => server
        [datetime] => server
    )

[1] => Array
    (
        [pid] => 12343
        [name] => serv3er
        [descr] => ser3ver
        [creator] => se3rver
        [datetime] => serve3r
    )

)

this is the result of when i array_diff_assoc(b, a)

 Array
(
[1] => Array
    (
        [pid] => 12343
        [name] => serv3er
        [descr] => ser3ver
        [creator] => se3rver
        [datetime] => serve3r
    )

)

but when i compare a to b it is blank.

I would even like to go further only compare the first value of the array (pid in this case), and if its not in both return that a row

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345

2 Answers2

1

Check out the second answer here: array_diff() with multidimensional arrays. ( just about the only variation you haven't tried ;-) )

Using array_udiff (http://us2.php.net/array_udiff) seems like the best solution.

Community
  • 1
  • 1
Jaap Moolenaar
  • 1,080
  • 6
  • 15
-1

This is the difference between these two functions : array_diff | array_diff_assoc

you can compare the results to understand

$a1 = array("a" => "red", "b" => 22, "c" => "blue", "d" => "yellow");

$a2 = array("e" => "red", "b" => 33, "g" => "blue");

$result = array_diff($a1, $a2);
  
  print_r($result);  
  
/*  Output:

  Array
  (
      [b] => 22
      [d] => yellow
  )*/
   
   echo "\n";
   echo "\n";
   
$result = array_diff_assoc($a1, $a2);
  
  print_r($result); 

/*
  Array
  (
      [a] => red
      [b] => 22
      [c] => blue
      [d] => yellow
  )
*/ 

check in the link : https://onecompiler.com/php/3xu58w6mx

More explanations

The array_diff_assoc() function is used to compare an array against another array and returns the difference. Unlike array_diff() the array keys are also used in the comparison. The first array which will be compared with other arrays. Compared with the first array.

Rachid Loukili
  • 623
  • 6
  • 15