Search code examples
phparraysfilteringassociative-arrayarray-difference

array_diff to compare two associative arrays


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,
);

Solution

  • I believe you want to use array_diff_assoc

    http://www.php.net/manual/en/function.array-diff-assoc.php