0

Possible Duplicate:
Invalid argument supplied for foreach()

How can I not to loop the key with empty value in an array?

For instance, I want to skip [submit] => or [submit] => add from being looped in foreach,

Array ( 
[cart] => Array ( [1386638969582999] => Array ( [quantity_stock] => 10 ) ) 
[submit] => 
) 

the loop,

foreach ($_POST as $index => $array_items) 
{
    foreach($array_items as $id => $item)
    {
        $cart->add_item($id);
    }


}

I get this error,

Warning: Invalid argument supplied for foreach() in C:... on line xx

Community
  • 1
  • 1
Run
  • 54,938
  • 169
  • 450
  • 748

1 Answers1

3

Use is_array() to test if the key refers to an array:

foreach ($_POST as $index => $array_items) 
{
    // $_POST['submit'] won't be processed since it isn't an array
    if (is_array($array_items))
    {
      foreach($array_items as $id => $item)
      {
          $cart->add_item($id);
      }
    }
}
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390