Jump to content

Invalid argument supplied for foreach()


resting

Recommended Posts

Hi guys,

 

I wrote the following codes and it seems to display well from the print_r statement.

however there is this Warning: Invalid argument supplied for foreach() in *** on line 26 which i dont understand why its there.

 

it is caused by this statement foreach($sel as $sel_product => $id) but all arguments supplied are valid to what i know.

 

would be glad if someone here helps me out. thanks.

 

The statements:

if(is_array($_REQUEST)){

foreach ($_REQUEST as $sel){

foreach($sel as $sel_product => $id)

{

echo "Selected product:" . $sel_product. "<br>";

echo "Selected id:" . $id. "<br>";

}

        }

}

 

The print out:

Array

(

    [Food] => Array

        (

            [1] => 1

            [2] => 4

        )

 

    [Drink] => Array

        (

            [4] => 1

        )

 

    [phpSESSID] => s51v7i378hvp8im5u2smgbgo07

)

 

Selected product:1

Selected id:1

Selected product:2

Selected id:4

Selected product:4

Selected id:1

 

Warning: Invalid argument supplied for foreach() in *** on line 26

Link to comment
Share on other sites

you should use $_GET / $_POST instead of $_REQUEST.

 

I don't know exactly how the script works, but I'm guessing not all of the $_REQUEST elements are arrays - hence the second foreach is causing an error..

 

try

 

if(is_array($_REQUEST)){
   foreach ($_REQUEST as $sel){
if (is_array($sel) {
      foreach($sel as $sel_product => $id)
      {
         echo "Selected product:" . $sel_product. "<br>";
         echo "Selected id:" . $id. "<br>";
      }
}
        }
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.