I have two arrays one which has all the data, and another associative array of the keys I would like to remove.
$all_data = array(
'one' =>array(
'one_one'=>array(
'one_one_one'=>'value',
'one_one_two'=>'value',
),
'one_two'=> array(
'one_two_one'=>'value',
'one_two_two'=>'value',
)
),
'two' =>array(
'two_one'=>array(
'two_one_one' =>'value',
'two_one_two' =>'value',
),
'two_two'=>array(
'two_two_one'=>'value',
'two_two_two'=>'value',
)
)
);
$remove_elements = array(
'one' =>array(
'one_one',
'one_two'=> array(
'one_two_one',
)
),
'two'
);
The output I would like would look like this.
$result = array(
'one' =>array(
'one_two'=> array(
'one_two_two'=>'value',
)
)
);
I have tried array_diff but I get a "Array to string conversion" error, I assume this is because they are nested arrays.
i created a function but ii cant get it to work.
function arrayRecursiveUnset($array_1, $array_2) {
foreach ($array_1 as $array_1_key => $array_1_value) {
if (array_key_exists($array_1_key, $array_2)) {
if (is_array($array_1_value)) {
$array_1[$array_1_key] = arrayRecursiveUnset($array_1_value, $array_2[$array_1_key]);
} else {
unset($array_1[$array_1_key]);
}
}
}
return $array_1;
}