Eg:
$_SESSION['1'] = 'username'; // works
$_SESSION[1] = 'username'; //doesnt work
I want to store session array index as array index. So that o/p is :
Array(
[1] => 'username'
)
Yes, PHP supports arrays as session variables.
We can create the session by writing session_start() and destroy the session by using session_destroy(). You can access the session variable by writing $_session[“name”]. Let us understand how the session works from the following examples. Example 1: In the following, you can create the session by entering the name.
PHP $_SESSION is an associative array that contains all session variables. It is used to set and get session variable values.
$_SESSION
can only be used as an associative array.
You could do something like this though:
$_SESSION['normal_array'] = array();
$_SESSION['normal_array'][0] = 'index 0';
$_SESSION['normal_array'][1] = 'index 1';
Personally, I'd just stick with the associative array.
$_SESSION['username'] = 'someuser';
Or
$_SESSION['username_id'] = 23;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With