0

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 :)

2 Answers 2

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

or:

$array = array_filter($array, function ($i) { return $i['qty'] > 0; });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks so much, this worked great (I used the second example) :)
2

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

Hope it helps

1 Comment

Thanks muchly, I think deceze got in first tho +1 for your help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.