How to echo a session name in a foreach loop in PHP?

I view my session variables. I was able to repeat the session values, but would also like to repeat the session name corresponding to this value.

How can I echo the name of a session variable every time it ends?

This is the code I have:

foreach($_SESSION as $value) {
    echo  'Current session variable is: ' . $value . '<br />';
}
+3
source share
4 answers

It?

foreach($_SESSION as $key => $value) {
    echo  'Current session variable ' . $key . ' is: ' . $value . '<br />';
}
+9
source

Using a loop, foreachyou can get the names of both keys and the corresponding values ​​using this syntax:

foreach ($your_array as $key => $value) {
    // Use $key for the name, and $value for the value
}

So in your case:

foreach($_SESSION as $name => $value) {
    echo  'Current session variable is: ' . $value . ' ; the name is ' . $name . '<br />';
}
+3
source

foreach . $var => $val $val - , , $val - , .

foreach($_SESSION as $key => $value) {
    echo  'Session variable ' . $key . ' is: ' . $value . '<br />';
}
+1

:

foreach($_SESSION as $k => $v) 
{
   echo 'Variable ' . $k . ' is ' . $v . '<br />' 
}
+1

Source: https://habr.com/ru/post/1731327/


All Articles