Jump to content

[SOLVED] Help with foreach() multidimensional array


xander85

Recommended Posts

Hi All,

 

I did a search for other foreach() problems and cant seem to find a solution that fits my syntax, probably because I'm a newbie. Here is my code:

 

if($submit)
{

$_SESSION['firstname'] = $_POST['firstname'];
$_SESSION['lastname'] = $_POST['lastname'];
$_SESSION['id'] = $_POST['id'];

if(!isset($_SESSION['box']))
{
	$CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']);
	$_SESSION['box'] = $CartItem;
}
else
{
	$CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']);
	array_push($_SESSION['box'], $CartItem);
}

echo sizeof($_SESSION['box']);

?>
<table width="100%"  border="1" cellspacing="0" cellpadding="0">

<?
foreach($_SESSION['box'] as $CartItem[]=>$items)
{
echo "<tr>";
foreach($items as $item)  *****Line 32*******
	{
	echo "<td>$item</td>";
	}
echo "</tr>";
}



?>
</table>

 

I get this error:

 

Warning: Invalid argument supplied for foreach() in /home/content/n/g/c/ngcc1/html/yui/yui/examples/container/test.php on line 32

 

It is displayed three times; however, my current array is 27 elements.

 

change

	if(!isset($_SESSION['box']))
{
	$CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']);
	$_SESSION['box'] = $CartItem;
}
else
{
	$CartItem = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']);
	array_push($_SESSION['box'], $CartItem);
}

to

$_SESSION['box'][] = array ($_SESSION['firstname'], $_SESSION['lastname'], $_SESSION['id']);

When you setup the $_SESSION['box'] array, you are not setting up a multi dimension array. Instead you are setting up a 2d array. Only until the else block gets executed $_SESSION['box'] becomes a multidimensional array.

 

Eg: Whn you first run that code and $_SESSION['box'] is not set. It'll create following array:

Array
(
    [0] => username_here
    [1] => password_here
    [2] => id_here
)

 

When that block of code is executed again, the else part will execute as $_SESSION['box'] now exists. However when it is executed the array will turn into this:

Array
(
    [0] => username_here
    [1] => password_here
    [2] => id_here
    [3] => Array
        (
            [0] => username_here
            [1] => password_here
            [2] => id_here
        )

)

$_SESSION['box'] becomes an multidimensional array at index 3. This is the problem.

 

To fix it you'll need to do this:

Change this line:

$_SESSION['box'] = $CartItem;

To:

$_SESSION['box'][] = $CartItem;

 

However you will need to unset the first 3 keys in order for your code to work. Or you could just fix the first 3 keys for the box session. with this:

if(!is_array($_SESSION['box'][0]))
    {
        $_SESSION['box'][0] = array($_SESSION['box'][0], $_SESSION['box'][1], $_SESSION['box'][2]);

        unset($_SESSION['box'][1], $_SESSION['box'][2]);
    }

You can remove the above three lines once you have run the code. This will just fix the session format.

I figured it out thanks!

 

It actually turns out the warnings were from the first three entries into the session array that were not inserted corrected and had not been unset or the session was destroyed. I destroyed the session and it worked out fine.

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.