简体   繁体   English

PHP数组需要帮助

[英]php array help needed

i need some help with a php array, I need to remove the array if the qty is 0, but I'm not sure how to do this.. my array is: 我需要有关php数组的一些帮助,如果数量为0,则需要删除该数组,但是我不确定如何执行此操作。我的数组是:

Array
(
    [2_Neutral] => Array
        (
            [qty] => 0
            [id] => 2_Neutral
        )

    [2_Honey] => Array
        (
            [qty] => 3
            [id] => 2_Honey
        )

)

As you can see 2_Neutral->qty is 0, so I would need this removed (everything to do with 2_Neutral) leaving only the 2_Honey information: 如您所见2_Neutral-> qty为0,所以我需要将其删除(与2_Neutral无关),仅保留2_Honey信息:

[2_Honey] => Array
        (
            [qty] => 3
            [id] => 2_Honey
        )

Any help would be greatly appreciated :) 任何帮助将不胜感激 :)

foreach ($array as $key => $value) {
    if ($value['qty'] <= 0) {
        unset($array[$key]);
    }
}

or: 要么:

$array = array_filter($array, function ($i) { return $i['qty'] > 0; });

foreach($yourArr as $key => $val) {
   if(empty($val['qty'])) {
      unset($yourArr[$key]);
   }
}

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM