Page 1 of 1

Need help with Array in Session...

Posted: Wed Jun 25, 2003 5:57 am
by iamler
It always show Array Array Array Array.
Can any one help me???

Thank you....

Code: Select all

<?php
session_start();

$info = array("coffee", "brown", "caffeine"); 

$_SESSION[wow] = each ($info);

echo session_id();
echo "<br>";
echo session_encode();
echo "<br><br>";

if(isset($_SESSION[wow]))
{
	echo "It registered: <br>";

	while (list ($drink, $color, $power) = each ($_SESSION[wow])) 
		{
	    	echo $_SESSION[wow]."<br>\n";
		} 
}
else
{	
	echo "It not registered";
}

?>
mod_edit: added

Code: Select all

tags[/size]

Posted: Wed Jun 25, 2003 6:18 am
by volka
Let's start with a link to http://php.net/each
echo $_SESSION[wow]."<br>\n";
why do you print the array? You want the values.
Please read http://www.php.net/manual/en/language.types.array.php#language.types.array.donts
maybe foreach() is simpler to use

Code: Select all

<?php
session_start();
$_SESSION['wow'] = array();
$info = array('drink'=>'coffee', 'color'=>'brown', 'power'=>'caffeine');
$_SESSION['wow'][] = $info; // appending the array $info to $_SESSION['wow']
?><html>
	<body>
		<fieldset>
			<legend>session_id</legend>
			<?php echo session_id(); ?>
		</fieldset>
		<fieldset>
			<legend>session_encode</legend>
			<?php echo session_encode(); ?>
		</fieldset>
<?php		
if(isset($_SESSION['wow']))
{
?>
		It registered: <br />
		<pre><?php print_r($_SESSION['wow']); ?></pre>
<?php
	foreach($_SESSION['wow'] as $wowEntry)
		echo $wowEntry['drink'], ' - ', $wowEntry['color'], ' - ', $wowEntry['power'], '<br />';
}
else
{   
   echo "It not registered";
}
?>
	</body>
</html>

Posted: Wed Jun 25, 2003 7:07 am
by pootergeist
or you might like to try serialize - unserialize to place the array within a session variable

Code: Select all

$info = array('drink'=>'coffee', 'color'=>'brown', 'power'=>'caffeine');
$_SESSION['wow'] = serialize($info); // make string from array
....
$wow = unserialize($_SESSION['wow']); // make array from string
echo $wow['color'];

Thank you everyone for your help...

Posted: Wed Jun 25, 2003 7:57 am
by iamler
...