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.

 

Link to comment
Share on other sites

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']);

Link to comment
Share on other sites

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.

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.