I'm having trouble using array_diff correctly. I've got 2 arrays:
$arr_1 = array(
0 => array('name' => 'Day Rate 2', 'from' => 1200, 'to' => 1400),
1 => array('name' => 'Day Rate 2', 'from' => 2000, 'to' => 2000),
);
$arr_2 = array(
0 => array('name' => 'Day Rate 2', 'from' => 0, 'to' => 1000),
1 => array('name' => 'Day Rate 2', 'from' => 1200, 'to' => 1400),
2 => array('name' => 'Day Rate 3', 'from' => 2000, 'to' => 4000),
);
I want to get the values in $arr_2 that are not present in $arr_1. I want it to return this:
0 => array('name' => 'Day Rate 2', 'from' => 0, 'to' => 1000)
To compare them, I first serialized the values of each item and created these two serialized arrays, which I can use to compare, using array_diff.
foreach ($arr_1 as $key => $val) {
$arr_1_simple[$key] = serialize(array($val['from'], $val['to']));
}
foreach ($arr_2 as $key => $val) {
$arr_2_simple[$key] = serialize(array($val['from'], $val['to']));
}
Array
(
[0] => a:2:{i:0;i:1200;i:1;i:1400;}
[1] => a:2:{i:0;i:2000;i:1;i:2000;}
)
Array
(
[0] => a:2:{i:0;i:0;i:1;i:1000;}
[1] => a:2:{i:0;i:1200;i:1;i:1400;}
[2] => a:2:{i:0;i:2000;i:1;i:4000;}
)
Since a:2:{i:0;i:1200;i:1;i:1400;}
and a:2:{i:0;i:2000;i:1;i:4000;}
are found in both $arr_1 and $arr_2,the odd one out is a:2:{i:0;i:0;i:1;i:1000;}
, which is what I thought array_diff would return.
However, the result that I'm getting is:
print_r(array_diff($arr_2_simple, $arr_1_simple));
Array
(
[0] => a:2:{i:0;i:0;i:1;i:1000;}
[2] => a:2:{i:0;i:2000;i:1;i:4000;}
)
Can anyone tell me why a:2:{i:0;i:2000;i:1;i:4000;}
is getting returned? I want all the items in $arr_2 that are not in $arr_1. How do I get this?