|
Posted by Janwillem Borleffs on 01/07/08 08:18
Peter Pei schreef:
> With a usual (well bothing should be unusual) array,
> $a[] = "a"; $a[] ="b" sets the first two elements of the array.
>
> I expect somthing like this to work in exactly the same way:
> start_session();
> $_SESSION[] = $_POST["a"];
> $_SESSION[] = $_POST["b"];
>
> So what I meant is that run this page, PHP should set the first 4
> elements, but what actually happened was the second round overwrites the
> first one, and the array ends up only have the first 2 elements set.
>
The $a[] = 'value' approach only works when $a is an indexed array. The
$_SESSION array is associative, meaning it works with keys and values.
The following will work as you expect:
$_SESSION['key'][] = $_POST["a"];
$_SESSION['key'][] = $_POST["b"];
Now you can do $_SESSION['key'][index] to retrieve the appropriate value.
JW
Navigation:
[Reply to this message]
|