Reputation: 1421
I am trying to filter a multi-dimension array by removing subarrays where the permission
value is no
.
My array:
$array = array(
array(
'name' => 'dashboard',
'permission' => 'yes'
),
array(
'name' => 'Purchase Orders',
'permission' => 'yes',
'dropdown' => array(
array(
'name' => 'View Complete',
'permission' => 'yes'
),
array(
'name' => 'New PO',
'permission' => 'no'
)
)
),
array(
'name' => 'dashboard',
'permission' => 'no'
)
);
This is my desired result: (notice all groups with permission=>'no'
have been fully removed)
$array = array(
array(
'name' => 'dashboard',
'permission' => 'yes'
),
array(
'name' => 'Purchase Orders',
'permission' => 'yes',
'dropdown' => array(
array(
'name' => 'View Complete',
'permission' => 'yes'
)
)
)
);
Using array_filter()
with a callback function does this very simply on the first level, but I cannot work out a simple solution for doing it on every level.
At the moment my solution is looping and unsetting each key, but it needs to know the exact structure of the array and feels quite messy.
Upvotes: 3
Views: 62
Reputation: 47904
Here is a method with recursion. A few inline comments to help explain, but there isn't much to explain that the basic functions don't express inherently.
Code: (Demo)
$array = array(
array(
'name' => 'dashboard',
'permission' => 'yes'
),
array(
'name' => 'Purchase Orders',
'permission' => 'yes',
'dropdown' => array(
array(
'name' => 'View Complete',
'permission' => 'yes'
),
array(
'name' => 'New PO',
'permission' => 'no'
)
)
),
array(
'name' => 'dashboard',
'permission' => 'no'
));
function recursive_filter($array){
foreach($array as $k=>&$subarray){ // make modifiable by reference
if(isset($subarray['permission']) && $subarray['permission']=='no'){ // check that this element exists before trying to access it
unset($array[$k]); // remove subarray
}elseif(isset($subarray['dropdown'])){ // check that this element exists before trying to access it
$subarray['dropdown']=recursive_filter($subarray['dropdown']); // recurse
}
}
return $array;
}
var_export(recursive_filter($array));
Output:
array (
0 =>
array (
'name' => 'dashboard',
'permission' => 'yes',
),
1 =>
array (
'name' => 'Purchase Orders',
'permission' => 'yes',
'dropdown' =>
array (
0 =>
array (
'name' => 'View Complete',
'permission' => 'yes',
),
),
),
)
Upvotes: 1
Reputation: 3697
Little bit complicated. This works only if the array is not getting deeper than the example you gave.
foreach($array as $key => $item) {
if(isset($item['permission']) && $item['permission'] == 'no') {
unset($array[$key]);
}
if(isset($item['dropdown'])) {
foreach($item['dropdown'] as $key2 => $item2) {
if(isset($item2['permission']) && $item2['permission'] == 'no') {
unset($array[$key]['dropdown'][$key2]);
}
}
}
}
Upvotes: 1