【问题标题】:Remove empty values from array within array using php使用php从数组中的数组中删除空值
【发布时间】:2013-11-20 11:00:51
【问题描述】:

如何从该组数组中删除 Array[1],因为它在两个字段中具有空值???。我尝试使用 array_chunk 和 array_filter 但没有得到。如果有任何帮助,那就太好了!!!!!!

Array
    (
        [0] => Array
            (
                [product] => 3
                [processing_id] => 33
                [quantity] => 50
            )

        [1] => Array
            (
                [product] => 
                [processing_id] => 33
                [quantity] => 
            )

    )

【问题讨论】:

    标签: php arrays codeigniter


    【解决方案1】:
    $array = array_filter($array, function (array $entry) {
        return $entry['product'] && $entry['quantity'];
    });
    

    将回调修改为确切的条件。只有当productquantity 都是真实时,才会保留这里的条目。

    【讨论】:

      【解决方案2】:
      array_filter($array)
      
      array_filter: "If no callback is supplied, all entries of input equal to FALSE will be removed." This means that elements with values NULL, 0, '0', '', FALSE, array() will be removed too.
      
      The other option is doing
      
      array_diff($array, array(''))
      
      which will remove elements with values NULL, '' and FALSE.
      

      【讨论】:

        猜你喜欢
        • 2016-05-23
        • 1970-01-01
        • 1970-01-01
        • 2013-07-25
        • 1970-01-01
        • 2017-03-16
        相关资源
        最近更新 更多