Jump to content

echoing a $_SESSION['array']


JPark

Recommended Posts

I am trying to display the contents of an array and having a problem.  I am getting an array of states (and state abbreviations) from a form and throwing this into a SESSION array ($_SESSION['states'] = $_POST['states'];)

 

Later, I want to echo these choices back, without the state abbreviations.

 

Let's say the customer gives me Maryland-MD, Virginia-VA and Delaware-DE.

 

If I use

foreach($_SESSION['states'] as $key=>$value)
	{
	$stateName = explode("-",$_SESSION['states']);
	echo $stateName[0].' <br />';
	}
echo "</blockquote>";

. I get

 

Notice: Array to string conversion in /vs/webdev/docs/econ/notifyme/state_entry.php on line 469

Array

 

Notice: Array to string conversion in /vs/webdev/docs/econ/notifyme/state_entry.php on line 469

Array

 

What am I doing wrong?  What should I do?

 

Thanks,

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/170139-echoing-a-_sessionarray/
Share on other sites

when you use a foreach loop, you refer to each entry in the variable as the value variable you use. so instead of using $_SESSION['states'] in your code, you have to use $value.

 

so:

foreach($_SESSION['states'] as $key=>$value)
      {
      $stateName = explode("-",$value);
      echo $stateName[0].' <br />';
      }
   echo "</blockquote>";

 

should work for what you're doing

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.