删除嵌套数组中的元素

时间:2013-11-10 16:19:22

标签: php multidimensional-array array-unset

我有一个PHP数组如下:

$errors = array('Price'=>array('not a positive decimal number'=> 1), 'TaxYear'=>array('not a positive integer'=>1, 'not 4 digits'=>1), 'Address'=>array(''=>1), 'State'=>array('not 2 letters'=>1, ''=>1), 'ListDate'=>array(''=>1, 'some test'=>1, ''=>1));

echo '$errors:<pre>' . print_r($errors,1) . '</pre>';

Array
(
[Price] => Array
    (
        [not a positive decimal number] => 1
    )

[TaxYear] => Array
    (
        [not a positive integer] => 1
        [not 4 digits] => 1
    )

[Address] => Array
    (
        [] => 1
    )

[State] => Array
    (
        [not 2 letters] => 1
        [] => 1
    )

[ListDate] => Array
    (
        [] => 1
        [some test] => 1
    )

)

目标是从这个创建另一个数组,如下所示:

Array
(
[Price] => Array
    (
        [not a positive decimal number] => 1
    )

[TaxYear] => Array
    (
        [not a positive integer] => 1
        [not 4 digits] => 1
    )

[State] => Array
    (
        [not 2 letters] => 1
    )

[ListDate] => Array
    (
        [some test] => 1
    )

)

基本上,需要删除嵌套数组中以[]作为元素名称的任何元素。如果外部数组中的任何键只有1个错误并且该错误的元素名称为[],则外部数组中的键也需要被删除(有关此示例,请参阅示例中的[Address])。实现这一目标的最佳方法是什么?

4 个答案:

答案 0 :(得分:1)

你可以使用这样的东西来选择性地复制元素:

$filtered = array();
foreach($errors as $category => $pairs) {
    foreach($pairs as $key => $value) {
        if($key != '') {
            $filtered[$category][$key] = $value;
        }
    }
}

答案 1 :(得分:1)

也许是这样的?

$return = array_filter($errors, function(&$value){
        if(array_key_exists('',$value)){
                unset($value['']);
        }
        return count($value);
});

答案 2 :(得分:0)

这是一个答案(使用相同的数组):

$errors = array('Price'=>array('not a positive decimal number'=> 1), 'TaxYear'=>array('not a positive integer'=>1, 'not 4 digits'=>1), 'Address'=>array(''=>1), 'State'=>array('not 2 letters'=>1, ''=>1), 'ListDate'=>array(''=>1, 'some test'=>1, ''=>1));

foreach($errors as $key1 => $err1) {
    foreach($err1 as $key2 => $err2) {
        if($key2 == '') {
            unset($errors[$key1][$key2]);
        }
    }
}

var_dump($errors);

输出

array (size=5)
  'Price' => 
    array (size=1)
      'not a positive decimal number' => int 1
  'TaxYear' => 
    array (size=2)
      'not a positive integer' => int 1
      'not 4 digits' => int 1
  'Address' => 
    array (size=0)
      empty
  'State' => 
    array (size=1)
      'not 2 letters' => int 1
  'ListDate' => 
    array (size=1)
      'some test' => int 1

答案 3 :(得分:0)

$errors = array('Price'=>array('not a positive decimal number'=> 1), 'TaxYear'=>array('not a positive integer'=>1, 'not 4 digits'=>1), 'Address'=>array(''=>1), 'State'=>array('not 2 letters'=>1, ''=>1), 'ListDate'=>array(''=>1, 'some test'=>1, ''=>1));


$filterOnKeys = function($x)
                {
                    $r = array(); 
                    foreach($x as $k=>$v) 
                        if($k) $r[$k]=$v; 
                    return $r;
                };

$return = array_filter(array_map($filterOnKeys, $errors));

echo '$return:<pre>' . print_r($return,true) . '</pre>';