I'm confusing array_diff behavior
why genre don't exist on diff array? Do you know how to resolve the matter?
-code
<?php
$array1 = array
(
'value01' => '0',
'value02' => 'v2',
'genre' => '1',
'type' => 'text',
'contry' => 'us',
'data' => '1',
);
$array2 = array
(
'value01' => 'v1',
'value02' => 'v2',
'genre' => '0',
'type' => 'text',
'contry' => 'canada',
'data' => '1',
);
print_r(array_diff($array1,$array2));
My result:
Array
(
[contry] => us
)
But I expect:
Array
(
[value01] => 0,
[genre] => 1,
[contry] => us,
);
The array_diff() function compares the values of two (or more) arrays, and returns the differences. This function compares the values of two (or more) arrays, and return an array that contains the entries from array1 that are not present in array2 or array3, etc.
The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
Now, to check whether two arrays are equal or not, an iteration can be done over the arrays and check whether for each index the value associated with the index in both the arrays is the same or not. PHP has an inbuilt array operator( === ) to check the same but here the order of array elements is not important.
I believe you want to use array_diff_assoc
http://www.php.net/manual/en/function.array-diff-assoc.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With