Re: SESSION array problems

From: Date: Thu, 02 Oct 2008 05:42:07 +0000
Subject: Re: SESSION array problems
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-281355@lists.php.net to get a copy of this message
tedd wrote:
Hi gang: Apparently, there's something going on here that I don't understand -- this happens far too often these days. Here's a print_r($_SESSION); of the session arrays I'm using:
    [user_id] => Array
        (
            [0] => 6156
            [1] => 7030
            [2] => 656
        )
    [first_name] => Array
        (
            [0] => Diane
            [1] => Fred
            [2] => Helen
        )
    [last_name] => Array
        (
            [0] => Cable
            [1] => Cago
            [2] => Cahalan
The following is how I tried to access the data contained in the $_SESSION arrays: $num_users = count($_SESSION['user_id']); for ($i = 0; $i < $num_users; $i++)
    {
    $last_name = $_SESSION['last_name'][$i];
    $first_name = $_SESSION['first_name'][$i];
    echo("<p>$last_name, $first_name</p>");
    }
The only thing that came out correct was the first echo. The remaining echos had no values for $first_name or $last_name. What's happening here? Cheers, tedd PS: I'm open to other suggestions as to how to do this.
Why don't you echo what you are trying to access for each loop for ($i = 0; $i < $num_users; $i++)
    {
    print_r($_SESSION['last_name'][$i]);
    print_r($_SESSION['first_name'][$i]);
    }
Does the above return to you what you would expect? An alternate to what you are doing here would be this. foreach ( $_SESSION['user_id'] AS $i=>$id)
    {
    print_r($id);
    print_r($_SESSION['last_name'][$i]);
    print_r($_SESSION['first_name'][$i]);
    }
How about this? Jim Lucas

« previous php.general (#281355) next »