Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $arr1 = array(
- 'key1' => 2,
- 'key2' => 4,
- 'key3' => array(1, 2,
- array(1,4,3,5,
- array(10, 13, 20)),
- 7),
- 'key4' => 1,
- );
- $arr2 = array(
- 'key1' => 2,
- 'key2' => 5,
- 'key3' => array(1, 2,
- array(1,34,3,5,
- array(10, 15, 20)),
- 7),
- );
- function array_diff_recursive($a1, $a2) {
- $result = array();
- foreach( $a1 as $k=>$v ) {
- if (array_key_exists($k, $a2)) {
- if ( is_array($v) && is_array($a2[$k]) ) {
- $result[$k] = array_diff_recursive($v, $a2[$k]);
- } else {
- if ( $v !== $a2[$k] ) {
- $result[$k] = $v;
- }
- }
- } else {
- $result[$k] = $v;
- }
- }
- return $result;
- }
- $multi_diff = array_diff_recursive($arr1, $arr2);
- // Tests
- echo "<pre>";
- echo "Array 1:\n";
- print_r($arr1);
- echo "\n\nArray 2:\n";
- print_r($arr2);
- echo "\n\nMulti diff:\n";
- print_r($multi_diff);
- echo "</pre>";
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement