SoulieBaby
SoulieBaby

Reputation: 5471

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:

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_Honey] => Array
        (
            [qty] => 3
            [id] => 2_Honey
        )

Any help would be greatly appreciated :)

Upvotes: 0

Views: 46

Answers (2)

deceze
deceze

Reputation: 522016

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

or:

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

Upvotes: 5

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175


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

Hope it helps

Upvotes: 2

Related Questions