0

我有一个 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

4 回答 4

1

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

$filtered = array();
foreach($errors as $category => $pairs) {
    foreach($pairs as $key => $value) {
        if($key != '') {
            $filtered[$category][$key] = $value;
        }
    }
}
于 2013-11-10T16:23:14.957 回答
1

也许像这样?

$return = array_filter($errors, function(&$value){
        if(array_key_exists('',$value)){
                unset($value['']);
        }
        return count($value);
});
于 2013-11-10T16:27:26.853 回答
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>';
于 2013-11-10T20:34:29.297 回答
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
于 2013-11-10T16:25:51.000 回答