phparraysfilterassociative-arrayarray-difference

Get differences between two associative arrays


I'm confused by array_diff() behavior.

Why doesn't genre exist in my result array? Do you know how to resolve the matter?

<?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