Hi all,
I keep getting "Invalid argument supplied for foreach()" error message under certain circumstances.
I have two sets of arrays that I use to display a set of data. One of them is stored as a session variable, which chokes when being displayed.
display_cart($_SESSION['cart'], ...);
function display_cart($cart, ...)
{
foreach ($cart as $optionid => $qty)
{
blah blah
}
if (isset($_SESSION['EOs']))
{
error_log("\nESSION['EOs'] is set, num items = ".count($_SESSION['EOs']),3,"debug.txt");
error_log("\ndump_array(SESSION['EOs']) = ".dump_array($_SESSION['EOs']),3,"debug.txt");
foreach ($_SESSION['EOs'] as $optionid=>$row) // It chokes right here
{
blah blah
}
I put the error_logs to see what values I get. I get "1" for the count, and "argument is not array" as the returned value of dump_array().
What I do prior to this situation is:
I add a new product through a php page that does not touch SESSION['EOs'] at all. This page (a form) collects user input and creates a new product and adds it to the database. Immidiately after this, if I go to the page, that up to this point was displaying everything properly, I get the "invalid argument supplied..." error message.
I don;t know how an array can become a non-array!
I appreciate your help.
function dump_array($array)
{
if(is_array($array))
{
$size = count($array);
$string = '';
if($size)
{
$count = 0;
$string .= '{ ';
// add each element's key and value to the string
foreach($array as $var => $value)
{
$string .= "$var = $value";
if($count++ < ($size-1))
{
$string .= ', ';
}
}
$string .= ' }';
}
return $string;
}
else
{
// if it is not an array, just return it
error_log("\nargument is not an array",3,"debug.txt");
return $array;
}
}