数组 - 删除值为零的记录

时间:2014-12-11 14:47:00

标签: php arrays

我有一个数组我想删除所有记录,其中[数量] => 0

 Array ( 
 [0] => Array ( [id] => 1 [is_sub] => 0 [product] => Town [quantity] => 0 [price] => [total_item_price] => 0 [comments] => ) 

 [1] => Array ( [id] => 3 [is_sub] => 0 [product] => City [quantity] => 0 [price] => [total_item_price] => 0 [comments] => ) 
 )

 [2] => Array ( [id] => 3 [is_sub] => 0 [product] => News [quantity] => 1 [price] => [total_item_price] => 0 [comments] => ) 
 )

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

使用array_filter

$array = array_filter($array, function($x) {
    return $x['quantity'] != 0;
});

答案 1 :(得分:0)

我认为它是PHP。

你可以这样做:

foreach($arr as $key => $value) {
    if($value['quantity'] < 1) {
        unset($arr[$key]);
    }
}

$arr = array_filter($arr, function($val) {
    return ($val['quantity'] > 0);
});