6

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'
)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Angelin Nadar
  • 8,944
  • 10
  • 43
  • 53
  • It's legacy of register_globals stuff. Anyway, having such items in the session is a nonsense. Nested array **is** a way to go. – Your Common Sense Jan 01 '11 at 16:31
  • The ability to increment the session variable would have been useful because it's an easy way to store information about each previous request without overwriting. The way I worked around this limitation was the way @KenBoyer did below, by prepending some uniform string to the numeric value. It's a little bit of a hassle with some computational overhead. – Altimus Prime Sep 30 '21 at 02:41

3 Answers3

9

$_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;
Luke Antins
  • 2,010
  • 1
  • 19
  • 15
  • gd logic. AS session accepts only associative so storing under same array index & then under that to store inteeger values as array index – Angelin Nadar Jan 01 '11 at 16:25
6

I suspect this is probably because the $_SESSION array is purely an associative array. Additionally, as the PHP manual puts it:

The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore.

Incidentally, have you checked your error log for any NOTICE level errors? (You may have to enable this level.) Attempting to use a numeric key will quite possibly raise an error.

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • I got Warning: CI_Exceptions::include(application/errors/error_php.php) [ci-exceptions.include]: failed to open stream: No such file or directory in F:\wamp\www\blood\system\libraries\Exceptions.php on line 163 – Angelin Nadar Jan 01 '11 at 16:14
  • 1
    @Show me the code - That doesn't seem related. Chances are you don't have NOTICE level errors enabled. – John Parker Jan 01 '11 at 16:17
  • Yes the error is not related but i need to check whether NOTICE level errors is enabled ? – Angelin Nadar Jan 01 '11 at 16:26
  • 1
    @Show me the code - In an ideal world, yes, as it'll help to ensure you're writing clean code. That said, there may be rather a lot of warnings, which can be somewhat off putting. As a suggestion, you could leave NOTICE warnings on for local development, but disable them for production. (N.B.: Incidentally, production servers should *never* display errors.) – John Parker Jan 01 '11 at 16:32
  • 1
    @Show me the code - You need to check the error_reporting value in your php.ini - look for the "Error handling and logging" section. (You can also set these within a script via the error_reporting function, but I wouldn't recommend it.) – John Parker Jan 01 '11 at 16:50
1

You can also take this approach to save an array dimension:

$_SESSION['form_'.$form_id] = $form_name;

which might look like the following:

$_SESSION['form_21'] = 'Patient Evaluation';

as opposed to:

$_SESSION['form'][21] = 'Patient Evaluation';

which uses another array dimension.

KenBoyer
  • 41
  • 2
  • 7