【问题标题】:Cannot remove null elements from nested array无法从嵌套数组中删除空元素
【发布时间】:2018-08-29 15:04:10
【问题描述】:

我有一个数组,$row2

$row2 中存在两个数组。 $row2 的输出为:

Array
(
    [0] => Array
        (
            [Proposal_id] => 9
            [row] => 1
            [col1] => 2
            [col2] => 2
            [col3] =>
            [col4] =>
            [col5] =>
            [Type] => customtbl
            [Invoice_term] =>
            [Qoute] =>
            [Rate_per_hour] =>
            [Total] =>
        )

    [1] => Array
        (
            [Proposal_id] => 9
            [row] => 2
            [col1] => 3
            [col2] => 4
            [col3] =>
            [col4] =>
            [col5] =>
            [Type] => customtbl
            [Invoice_term] =>
            [Qoute] =>
            [Rate_per_hour] =>
            [Total] =>
        )

)

我想从数组中删除空元素,但我不能这样做。

我尝试了以下方法:

array_filter($row2);
array_filter($row2, function($var){return !is_null($var);});
array_diff($rows2, array("null", ""));

【问题讨论】:

  • 试试$row2 = array_map('array_filter', $row2);
  • @billyonecan 可行,但可能会产生意想不到的结果,因为它也会删除零。
  • @billyonecan 请发表您的评论作为对这个问题的回答
  • 不为我工作顺便说一句谢谢您的反馈

标签: php codeigniter


【解决方案1】:

array_map 中使用array_filter。 请记住,您可能想要运行不同的条件而不是 if($value),因为这不仅会验证空值,还会验证零和空字符串。

array_map(function($var) {
    return array_filter($var, function($value) {
        if ( !is_null($value) )
            return $value;
    });
},$row2);

【讨论】:

    【解决方案2】:

    如果您使用array_maparray_filter,我有一个单一的解决方案可以从多维数组中过滤出null 值,

        $array = [
        ['Proposal_id' => 9,
         'row' => 1,
         'col1' => 2, 
         'col2' => 2, 
         'col3' => null,
         'col4' => null,
         'col5' =>  null,
         'Type' => 'customtbl',
         'Invoice_term' => null,  
         'Qoute' => null,
         'Rate_per_hour' => null,  
         'Total' => null,
        ],
        [   
            'Proposal_id' => 9,
            'row' => 1,
            'col1' => 2, 
            'col2' => 2 ,
            'col3' => null,
            'col4' => null,
            'col5' =>  null,
            'Type' => 'customtbl',
            'Invoice_term' => null,  
            'Qoute' => null,
            'Rate_per_hour' => null,  
            'Total' => null,
        ]
    ];
    $af = array_filter(array_map('array_filter', $array));
    print '<pre>';
    print_r($af);
    print '</pre>';
    

    输出:

     Array
    (
        [0] => Array
            (
                [Proposal_id] => 9
                [row] => 1
                [col1] => 2
                [col2] => 2
                [Type] => customtbl
            )
    
        [1] => Array
            (
                [Proposal_id] => 9
                [row] => 1
                [col1] => 2
                [col2] => 2
                [Type] => customtbl
            )
    
    )
    

    演示https://eval.in/975240

    【讨论】:

    • @dani 你看过演示吗?它应该工作..为什么它不适合你?你能解释一下吗?
    • 您的代码正在使用您的数组,但不适用于我
    • 我正在从数据库中获取数据,这可能是行不通的
    • 是的,这可能是一个原因。但是我已经根据问题代码上的数组结构发布了答案。祝你好运..
    猜你喜欢
    • 2022-07-19
    • 1970-01-01
    • 2021-12-11
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2013-11-22
    • 2019-09-13
    相关资源
    最近更新 更多