multidimensional array difference php
Please check if I understand you correctly then this code snippet can help to you solve your problem. I have tested it for your specified problem only. if there are other testcases for which you want to run this, you can tell me to adjust the code.
$a1 = array(
'a1' => array('a_name' => 'aaa', 'a_value' => 'aaaaa'),
'b1' => array('b_name' => 'bbb', 'b_value' => 'bbbbbb'),
'c1' => array('c_name' => 'ccc', 'c_value' => 'cccccc')
);
$a2 = array(
'b1' => array('b_name' => 'zzzzz'),
);
$result = check_diff_multi($a1, $a2);
print '<pre>';
print_r($result);
print '</pre>';
function check_diff_multi($array1, $array2){
$result = array();
foreach($array1 as $key => $val) {
if(isset($array2[$key])){
if(is_array($val) && $array2[$key]){
$result[$key] = check_diff_multi($val, $array2[$key]);
}
} else {
$result[$key] = $val;
}
}
return $result;
}
EDIT: added tweak to code.
this solution ah been very helpful to me I hope can help them in something, no matter what the array are in disarray.
function your_array_diff($arraya, $arrayb) {
foreach ($arraya as $keya => $valuea) {
if (in_array($valuea, $arrayb)) {
unset($arraya[$keya]);
}
}
return $arraya;
}
$a1 = Array
(
"0" => Array
(
"Empresa" => "TMC01",
"Paga" => "13/01/2015",
"ID" => "M2",
"Valor" => "200",
"Nombre" => "BONI"
),
"1" => Array
(
"Empresa" => "TMC01",
"Paga" => "13/01/2015",
"ID" => "M1",
"Valor" => "100",
"Nombre" => "SUELDO"
)
);
$b1 = Array
(
"0" => Array
(
"Empresa" => "TMC01",
"Paga" => "13/01/2015",
"ID" => "M1",
"Valor" => "100",
"Nombre" => "SUELDO"
),
"1" => Array
(
"Empresa" => "TMC01",
"Paga" => "13/01/2015",
"ID" => "M2",
"Valor" => "200",
"Nombre" => "BONI"
)
);
$resultado = your_array_diff($a1, $b1);
echo "<pre>";
echo print_r($resultado);
echo "</pre>";