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
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

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.