I am using array_udiff function on Multi-dimensional Arrays:
$old_list = Array ( [0] => Array ( [name] => John [src] => S ) [1] => Array ( [name] => Mary [src] => S ) )
$new_list = Array ( [0] => Array ( [name] =>John [src] => S ) [1] => Array ( [name] => Mary [src] => S ) [2] => Array ( [name] => Peter [src] => S ))
$differences = array_udiff($new_list, $old_list, function($a, $b) {
return strcmp($a["name"], $b["name"]);
});
print_r($differences);
The result is
Array ( [2] => Array ( [name] => Peter [src] => S ) )
How can I get the values Peter & S (name
and src
) from the variable $differences
?
You currently have one result, but there could be more, or none. So the best thing to do is to walk the outer array and then access the inner array:
foreach ($differences as $key => $difference) {
echo "The name for key {$key} is: {$difference["name"]}<br>";
echo "The source for key {$key} is: {$difference["src"]}<br>";
}
You didn't actually tell us what you wanted to do with the values Peter & S, so I just echoed them.
See: foreach