【问题标题】:Removing arrays from multi dimension array based on single values基于单个值从多维数组中删除数组
【发布时间】:2018-05-25 14:10:11
【问题描述】:

我正在尝试通过删除permission 值为no 的子数组来过滤多维数组。

我的数组:

$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'
    )
);

这是我想要的结果:(注意所有带有permission=>'no' 的组已被完全删除)

$array = array(
    array(
        'name' => 'dashboard',
        'permission' => 'yes'
    ),
    array(
        'name' => 'Purchase Orders',
        'permission' => 'yes',
        'dropdown' => array(
            array(
                'name' => 'View Complete',
                'permission' => 'yes'
            )
        )
    )
);

array_filter() 与回调函数一起使用在第一级上非常简单,但我无法找到一个简单的解决方案来在每个级别上都这样做。

目前我的解决方案是循环并取消设置每个键,但它需要知道数组的确切结构并且感觉很混乱。

【问题讨论】:

  • 是否总是深达 2 层?
  • 您可以递归并检查元素是否为is_array 的数组并检查权限值。这样你就不必知道结构有多深。

标签: php recursion multidimensional-array filter array-unset


【解决方案1】:

这是一个递归的方法。几个内联的 cmets 来帮助解释,但基本函数本身并没有表达出来,就不多解释了。

代码:(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));

输出:

array (
  0 => 
  array (
    'name' => 'dashboard',
    'permission' => 'yes',
  ),
  1 => 
  array (
    'name' => 'Purchase Orders',
    'permission' => 'yes',
    'dropdown' => 
    array (
      0 => 
      array (
        'name' => 'View Complete',
        'permission' => 'yes',
      ),
    ),
  ),
)

【讨论】:

    【解决方案2】:

    有点复杂。这仅在数组没有比您给出的示例更深时才有效。

    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]);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 2014-11-17
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2017-06-23
      相关资源
      最近更新 更多