Find the differences betwween two arrays
-
Hi all, I have a problem finding the differences between two arrays:
$first_array = array("a"=>"1", "b"=>"7", "c"=>"4", "d"=>"4");
$second_array = array("a"=>"1", "b"=>"10", "c"=>"10", "d"=>"4", "e"=>"7");
I'm trying to subtract the old array from the new one and find any new values. So the output should be:
$new_array = array("b"=>"3", "c"=>"6", "e"=>"7");
I tried using array_diff() and array_diff_assoc(). But they give me the wrong output. Any ideas? Thanks.
-
Hi all, I have a problem finding the differences between two arrays:
$first_array = array("a"=>"1", "b"=>"7", "c"=>"4", "d"=>"4");
$second_array = array("a"=>"1", "b"=>"10", "c"=>"10", "d"=>"4", "e"=>"7");
I'm trying to subtract the old array from the new one and find any new values. So the output should be:
$new_array = array("b"=>"3", "c"=>"6", "e"=>"7");
I tried using array_diff() and array_diff_assoc(). But they give me the wrong output. Any ideas? Thanks.
I'm unaware of any library function that would do that, however you can easily iterate over the first array using
foreach ($first_array as $key => $value) {...}
and then look up the key in the second array, and adjust the value. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
Merry Christmas and a Happy New Year to all.