How to differentiate 2 multidimensional arrays?
You can use the following code:
public static function check_diff_multi($array1, $array2){ //Get all the Keys from the 2nd Array. $keys = array(); foreach( $array2 as $key => $a ) { $keys = array_merge( $keys, array_keys( $a )); } //This will be the final array $result = array(); //Loop through First Array foreach( $array1 as $key => $a ) { $firstKey = key($a); //Get the Key if( in_array( $firstKey, $keys ) ) { //If this key exists in the 2nd array, then continue continue; } $result[] = $array1[$key]; //This key is not present in 2nd array, so add to output. } return $result; }
Please try to use like this $aray1 = Array ([0] => Array ( [Notifications] => /LMS/Notifications )[1] => Array ( [Clients] => /LMS/Clients )[2] => Array ( [Penalties] => /LMS/Penalties )[3] => Array ( [Payments] => /LMS/Payments )[4] => Array ( [Collection] => )[5] => Array ( [Missed Payments] => )[6] => Array ( [Applied Penalties] => ));$aray2 = Array ([0] => Array ( [Dashboard] => /LMS/Dashboard )[1] => Array ( [Notifications] => /LMS/Notifications )[2] => Array ( [Clients] => /LMS/Clients )[3] => Array ( [Penalties] => /LMS/Penalties )[4] => Array ( [Payments] => /LMS/Payments )[5] => Array ( [Profit] => /LMS/Profit )[6] => Array ( [Income] => /LMS/Income )[7] => Array ( [Outcome] => /LMS/Outcome )[8] => Array ( [Accounts] => /LMS/Accounts )[9] => Array ( [Collection] => )[10] => Array ( [Missed Payments] => )[11] => Array ( [Applied Penalties] => )[12] => Array ( [Group Names] => /LMS/Group-Names ));$printResult = get_diff_array($aray1, $aray2);function get_diff_array($aray1, $aray2) { $arrayResult = []; foreach ($aray1 as $key => $val) { if (isset($aray2[$key])) { if (is_array($val) && $aray2[$key]) { $arrayResult[$key] = get_diff_array($val, $aray2[$key]); } } else { $arrayResult[$key] = $val; } } return $arrayResult;}
Please try this cose:
public static function diffArray($array1,$array2){ $flag=0; foreach($array1 as $key1 => $value1){ foreach ($array2 as $key2 => $value2) { if($key1==$key2) $flag++; } if($flag==0) $array3[$key1]=$value1; $flag=0; } return $array3;}
Your output will be:
Array( [Dashboard] => /LMS/Dashboard [Profit] => /LMS/Profit [Income] => /LMS/Income [Outcome] => /LMS/Outcome [Accounts] => /LMS/Accounts [Group Names] => /LMS/Group-Names)