multidimensional array difference in php

Solutions on MaxInterview for multidimensional array difference in php by the best coders in the world

showing results for - "multidimensional array difference in php"
David
02 Jun 2018
1function array_diff_assoc_recursive($array1, $array2) {
2    $difference=array();
3    foreach($array1 as $key => $value) {
4        if( is_array($value) ) {
5            if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
6                $difference[$key] = $value;
7            } else {
8                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
9                if( !empty($new_diff) )
10                    $difference[$key] = $new_diff;
11            }
12        } else if( !array_key_exists($key,$array2) || $array2[$key] !== $value ) {
13            $difference[$key] = $value;
14        }
15    }
16    return $difference;
17}
18//----------------------
19$changes = array();
20if ($result= array_diff_assoc_recursive($new_value,$old_value)) {
21		$new  = array();
22		
23		foreach ($result as $key => $value) {
24			$new[$key] =  $value;
25		}
26		// indicate your UNIQUE ID you can use this when you want know which row you wanted to update in database 
27		$new['ID'] = $new_value['ID'];
28		$changes[] = $new	;
29}
30
31print_r($changes);
queries leading to this page
find difference between two multidimensional array in phpcompare 2 multidimensional arrays phptwo array difference in phpcompare multidimensional array phparray diff for multidimensional arrays phpphp multidimensional array difference with keysphp array diff multidimensionalphp array diff multidimensionalphp 2 array differencephp return difference between two arraysphp difference between multidimensional arrayphp array diff single dimensionalarray diff multidimensional phpphp return difference of multidimensional arrayscompare two multidimensional arrays and return common values phpdifference between two dimensional and three dimensional array in phpcompare multiple array and return the difference one phparray diff in multidimensional array phpphp get difference between two multidimensional arraysphp array difference multidimensionalhow to multidimantional check array difference in phpmultidimensional array difference in phpphp multidimensional array differencedifference between multiple array values in php e2 80 9cmultidimensional array difference in phpcompare two multidimensional array in phpdifference between two array in phpphp array difference between two arraysphp multidimensional array diffarray difference multidimensional phparray multidimensional differences phpphp array compare multidimensionalarray diff multidimensional php objecthow to compare two multidimensional array in phpphp compare two multidimensional arrays and get the similarphp compare two multidimensional arrays and get the differencephp compare 2 multidimensional arrays2d array difference phpcompare multidimensional arrays phparray diff php multidimensionalarray diff multidimensional php2 dimensional array value difference subtraction phphow to compare multidimensional array in phparray value array difference phpmultidimensional array difference in php