You are here: Re: stange behavior of $_SESSION « PHP Language « IT news, forums, messages
Re: stange behavior of $_SESSION

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация