简体   繁体   中英

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

知道了,这个示例中没有设置值。

您已经很接近了,但是您的设置行实际上并未分配任何值。

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

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();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM