I have two multidimensional arrays below.
1St Array
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
[1] => part2
)
)
)
2nd Array
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part2
)
)
)
I want to get the difference between the above arrays and the output should be like below
Desired Output
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
)
)
)
I have tried to get the output using the below function but not getting the proper output.
Function which I have used
function array_diff_assoc_recursive($array1, $array2) {
$difference=array();
foreach($array1 as $key => $value) {
if( is_array($value) ) {
if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $array2[$key]);
if( !empty($new_diff) )
$difference[$key] = $new_diff;
}
} else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
$difference[] = $value;
}
}
return $difference;
}
Output
Array
(
[ma] => Array
(
[mdesktopapps] => Array
(
[0] => part1
[1] => part2
)
)
)
My Answer:
Array
(
[0] => Array
(
[id] => 1
[name] => John
)
[1] => Array
(
[id] => 2
[name] => Jane
)
[2] => Array
(
[id] => 3
[name] => Alice
)
)
2nd Array
(
[0] => Array
(
[user_id] => 1
[user_name] => John
)
[1] => Array
(
[user_id] => 2
[user_name] => Jane
)
[2] => Array
(
[user_id] => 4
[user_name] => Bob
)
)__ _I want to get the difference of these two arrays based on the 'id' and 'user_id' keys. The expected output should be the third array below._3rd Array_(_ [0] => Array
(
[id] => 3
[name] => Alice
)
[1] => Array
(
[user_id] => 4
[user_name] => Bob
)
)
Rate this post
3 of 5 based on 8891 votesComments