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?

0

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, the combination of array_diff with array_column ist exactly what i looked for!
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
};

Comments

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 
)

Comments

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

2 Comments

It's best to lead people to a solution. Rather than just give it.
Thanks for the suggestion, I'll keep that in mind. I have also added the reference link, user can look into that for more specific information
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;
        }

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.