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
https://forums.phpfreaks.com/topic/164904-invalid-argument-supplied-for-foreach/
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>";
      }
}
        }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.