4

I have a form that submits the following data. I only want to select the data with QTY of 1 or more.

array (size=4)
  17 => 
    array (size=4)
      'id' => string '17' (length=2)
      'TicketName' => string 'General admission' (length=17)
      'price' => string '50.00' (length=5)
      'qty' => string '0' (length=1)
  18 => 
    array (size=4)
      'id' => string '18' (length=2)
      'TicketName' => string 'General admission' (length=17)
      'price' => string '50.00' (length=5)
      'qty' => string '2' (length=1)
  19 => 
    array (size=4)
      'id' => string '19' (length=2)
      'TicketName' => string 'General admission' (length=17)
      'price' => string '50.00' (length=5)
      'qty' => string '0' (length=1)
  20 => 
    array (size=4)
      'id' => string '20' (length=2)
      'TicketName' => string 'General admission' (length=17)
      'price' => string '50.00' (length=5)
      'qty' => string '0' (length=1)
    

I know I can use something like. I'm not sure how to filter by the qty key.

$arrays = array_filter($inputs, function($val) {
                return $val > 0;
          });
1

2 Answers 2

4

Each element of your array $input, is an array. So you need to access the value associated to the key qty:

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

1 Comment

Thanks, but when I run this code i'm getting a "Illegal string offset 'qty'" error
1
$array = array();    
foreach($main_array as $chunk){
  if($chunk['qty'] > 0 ){
    $array[] = $chunk;
  }
}
var_dump($array);

Comments

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.