2

i have 2 php Arrays and want to compare elements. examples:

$Array_A[0]["field"]=10; $Array_B[0]["field"]=10;
$Array_A[1]["field"]=20; $Array_B[1]["field"]=30;
$Array_A[2]["field"]=30; $Array_B[2]["field"]=40;
$Array_A[3]["field"]=40;

Array_Difference() should return 20

$Array_A[0]["field"]=10; $Array_B[0]["field"]=10;
$Array_A[1]["field"]=20; $Array_B[1]["field"]=20;
$Array_A[2]["field"]=30; $Array_B[2]["field"]=40;
$Array_A[3]["field"]=40;

Array_Difference() should return 30

For Case that there are more than 1 Difference i would Loop a Function which is finding and return the first found Difference.

What is "best-pratice" to do this Task?

snowball1
  • 23
  • 3

5 Answers5

1

simply, you use array_udiff to create a custom diff function.

This will enable you to access the multidimensional elements.

$result = array_udiff($array1, $array2, function($a, $b){
    return $a['field'] <=> $b['field'];  // replace the spaceship if not using php7
};
DevDonkey
  • 4,835
  • 2
  • 27
  • 41
1

You should use array_diff() combined with array_column.

array_diff(array_column($Array_A, 'field'), array_column($Array_B, 'field'))

array_diff - returns difference between two arrays

array_column - returns one column from multidimensional array

If you want to have only one result then you can use array_shift() which will take the first element from the beginning of an array

f.e

$diff = array_diff(array_column($Array_A, 'field'), array_column($Array_B, 'field'));
$firstDifference = array_shift($diff);
Robert
  • 19,800
  • 5
  • 55
  • 85
0

You should probably look at: array_diff

Example #1 array_diff() example

$array1 = array("a" => "green", "red", "blue", "red"); 
$array2 = array("b" => "green", "yellow", "red"); 
$result = array_diff($array1, $array2);

print_r($result); ?> Multiple occurrences in $array1 are all treated
the same way. This will output :

Array (
     [1] => blue 
)
laminatefish
  • 5,197
  • 5
  • 38
  • 70
0
$Array_A[0]["field"]=10; $Array_B[0]["field"]=10;
$Array_A[1]["field"]=20; $Array_B[1]["field"]=30;
$Array_A[2]["field"]=30; $Array_B[2]["field"]=40;
$Array_A[3]["field"]=40;

$result=your_array_diff($Array_A,$Array_B);
print_r($result);

function your_array_diff($arraya, $arrayb) {

    foreach ($arraya as $keya => $valuea) {
        if (in_array($valuea, $arrayb)) {
            unset($arraya[$keya]);
        }
    }
    return $arraya;
}

Output: Array ( [1] => Array ( [field] => 20 ) )

Ref: https://stackoverflow.com/a/35071068/2520628

Tejashwi Kalp Taru
  • 2,994
  • 2
  • 20
  • 35
0

like the previous answers suggested array_diff () will find missing values in the second array of its parameter. however, i guess in your case the position (key) is also important. in that case you could use a simple loop.

foreach ($array1 as $key => $value)
    if (array_key_exists ($key, $array2)
        if ($array1[$key]['field'] != $array2[$key]['field']){
           //do something
           break;
        }
wayneOS
  • 1,427
  • 1
  • 14
  • 20
  • This is the only answer which caters to the requirement of finding the FIRST missing value only. Using `array_diff()` is value-based -- this may be outperformed by `array_diff_key()` because key-comparisons are faster in php. Mind you, assigning the keys will have a computational cost though. Both `array_diff()` and `array_diff_key()` will traverse the entirety of both arrays (even after the first missing value is found). "Short circuiting" with `break` makes this answer is an ideal choice. As for the rest of this snippet, I am not sold that it is fit for use by researchers. – mickmackusa Sep 18 '21 at 09:05