I have two arrays that contain nested arrays also, where I want to compare them against each other. If the value in the first array is false, I want that key removed from the 2nd array.
1st array: $arrayOne:
'entities' => [
'number' => false,
'items' => [
[
'name' => false,
],
],
'totals' => [
'gross' => true,
'tax' => [
'amount' => true,
],
]
];
Then, I have the second array $arrayTwo:
'entities' => [
'number' => '8000000',
'items' => [
[
'name' => 'Bolt cutter',
],
[
'name' => 'Wire cutter',
],
],
'totals' => [
'gross' => 120.52,
'tax' => [
'amount' => 90.21,
],
]
];
As you can see in the first array, the keys: number and items.name is false. These two keys should not be in the final array, thus making it look like this:
'entities' => [
'items' => [],
'totals' => [
'gross' => 120.52,
'tax' => [],
]
];
How can I accomplish this with nested arrays? I have managed to do it for the "1st level" of the array using below:
foreach ($arrayOne as $key => $entity) {
if ($entity === false) {
unset($arrayTwo[$key]);
}
}
However, as said, this only handles the first level of the array and not the nested array(s).