1

Im using cakephp 2.0 and i have data submitted that i want to cleanup, the array structure is below how do i delete the element (quoteitem) where the quantity=null?

i have this but it doesnt work;

foreach($this->request->data['Quoteitem'] as $qi) {
 if($qi['quantity']==null){
 echo 'quantity is null,delete this quote item from array';                 
 unset($qi);
}       
}

structure of array called ($this->request->data)

Array
(
    [Quote] => Array
        (
            [customer_id] => 72
            [user_id] => 104                
        )

    [Range] => Array
        (
            [id] => 
        )

    [Quoteitem] => Array
        (
            [0] => Array
                (
                    [product_id] => 
                    [unitcost] => 
                    [quantity] => 1
                )

            [1] => Array
                (
                    [product_id] => 
                    [unitcost] => 
                    [quantity] => 22
                )

            [2] => Array
                (
                    [product_id] => 339
                    [unitcost] => 5
                    [quantity] => 
                )     

        )

)

2 Answers 2

5

You can remove it using the array keys:

foreach($this->request->data['Quoteitem'] as $key => $qi) {
   if($qi['quantity'] == null){
      echo 'quantity is null,delete this quote item from array';                 
      unset($this->request->data['Quoteitem'][$key]);
   }       
}

Note that this will create gaps in the array (nonexistent indexes), usually this won't be a problem but if it is you can re-index the array with array_values().

Sign up to request clarification or add additional context in comments.

Comments

1

Foreach makes a copy, try this:

foreach($this->request->data['Quoteitem'] as $key => $qi) {
   if($qi['quantity']==null){
      echo 'quantity is null,delete this quote item from array';                 
      unset($this->request->data['Quoteitem'][$key]);
   }       
}

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.