JasonDavis
JasonDavis

Reputation: 48933

How can I show session array items in PHP

How can I make something like this below work?

<?PHP

$_SESSION['signup_errors']['test1'];
$_SESSION['signup_errors']['test2'];
$_SESSION['signup_errors']['test3'];
$_SESSION['signup_errors']['test4'];

foreach ($_SESSION['signup_errors'] as $key => &$value) {
    echo $value;
}
?>

Warning: Invalid argument supplied for foreach()

Upvotes: 0

Views: 3303

Answers (4)

JasonDavis
JasonDavis

Reputation: 48933

Ahh I got it, I didn't have the values set in this example.

Upvotes: 2

Steve Wasiura
Steve Wasiura

Reputation: 798

Since you are not actually assigning any values to the elements in the session array in that section of code, $_SESSION is not returned as an array, therefore the foreach is receiving an empty variable and throwing an error.

If you are seeing the error message " Invalid argument supplied for foreach() " and you know the session does contain values that you've set, make sure the session is started, using the php command session_start();

then you will be able to loop through the session array and see the key & their values

Upvotes: 0

Paige Ruten
Paige Ruten

Reputation: 176635

It means you haven't assigned $_SESSION['signup_errors'] a value, meaning there were no errors I guess. You should put the following line above the error-checking code:

$_SESSION['signup_errors'] = array();

Upvotes: 0

Mark F
Mark F

Reputation: 151

You're pretty close, but your setup lines don't actually assign any values.

$_SESSION['signup_errors']['test1'] = 'value1';

Upvotes: 1

Related Questions