[SOLVED] list() with array in the Session?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

[SOLVED] list() with array in the Session?

Post by dashifen »

Greetings!

As part of a form-validation page I'm writing, I stuck the previously entered form data into the session when an error was encountered so that I can extract said data after redirecting to the form page and place it into the correct form fields. In the past I've always had lines like this:

Code: Select all

$title = $_SESSION['event_details']['title'];
$start_date = $_SESSION['event_details']['start_date'];
And the like. However, it occured to me today that I could probably perform a similar task with the list() construct. I tried this:

Code: Select all

list($title, $start_date) = $_SESSION['event_details'];
But received an error saying that there I had attempted to access an invalid offset (or something like that). I assumed that that meant that list() couldn't extract the values of the array within the $_SESSION array.

Any ideas on how to do get at those values with list()? If I use print_r() I can display them on the screen, so I know they're in there. Could it be that the array is associative and therefore doesn't have numerical indices? If so, anyone got a good way to convert associative to "normal" arrays or is there a list() construct that works on associative arrays?

Thanks!
-- Dash --
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Try:

Code: Select all

list($title, $start_date) = each($_SESSION['event_details']);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

try

Code: Select all

list($title,$start_date) = array_values($_SESSION['event_details']);
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Post by dashifen »

feyd wrote:try

Code: Select all

list($title,$start_date) = array_values($_SESSION['event_details']);
This works.

Thanks!!!
Post Reply