Re: Re: SESSION array problems

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Nathan Rixham wrote:
> tedd wrote:
>> At 3:41 PM -0400 10/1/08, tedd wrote:
>>>> What about:
>>>>
>>>> foreach ($_SESSION['user_id'] as $key => $value)
>>>> {
>>>>     $last = $_SESSION['last_name'][$key];
>>>>     $first = $_SESSION['first_name'][$key];
>>>>     echo "$last, $first";
>>>> }
>>>
>>> Jay:
>>>
>>> Close, it produced:
>>>
>>> Array, Array
>>> Array, Array
>>> Array, Array
>>>
>>> Cheers,
>>>
>>> tedd
>>
>>
>> My error, if produced.
>>
>>
>> Cable, Diane
>> a, i
>>
>> While print_r($_SESSION); JUST BEFORE IT produced:
>>
>> [user_id] => Array
>>     (
>>         [0] => 6156
>>         [1] => 7030
>>         [2] => 656
>>     )
>>
>> [first_name] => Array
>>     (
>>         [0] => Diane
>>         [1] => first name
>>         [2] => Helen
>>     )
>>
>> [last_name] => Array
>>     (
>>         [0] => Cable
>>         [1] => CagoEsogs-temp (forum)
>>         [2] => Cahalane
>>     )
>>
>> Now, what wrong with this picture?
>>
>> Cheers,
>>
>> tedd
>
> tedd wrote:
> > At 3:41 PM -0400 10/1/08, tedd wrote:
> >>> What about:
> >>>
> >>> foreach ($_SESSION['user_id'] as $key => $value)
> >>> {
> >>>     $last = $_SESSION['last_name'][$key];
> >>>     $first = $_SESSION['first_name'][$key];
> >>>     echo "$last, $first";
> >>> }
> >>
> >> Jay:
> >>
> >> Close, it produced:
> >>
> >> Array, Array
> >> Array, Array
> >> Array, Array
> >>
> >> Cheers,
> >>
> >> tedd
> >
> >
> > My error, if produced.
> >
> >
> > Cable, Diane
> > a, i
> >
> > While print_r($_SESSION); JUST BEFORE IT produced:
> >
> > [user_id] => Array
> >     (
> >         [0] => 6156
> >         [1] => 7030
> >         [2] => 656
> >     )
> >
> > [first_name] => Array
> >     (
> >         [0] => Diane
> >         [1] => first name
> >         [2] => Helen
> >     )
> >
> > [last_name] => Array
> >     (
> >         [0] => Cable
> >         [1] => CagoEsogs-temp (forum)
> >         [2] => Cahalane
> >     )
> >
> > Now, what wrong with this picture?
> >
> > Cheers,
> >
> > tedd
>
> in this case.. what's happened is:
> $_SESSION['first_name'] is a reference to a variable $first (or
> whatever is in your for loop)
> $_SESSION['last_name'] is a reference to a variable $last (or whatever
> is in your for loop)
>
> when you've set $last and $first to string's in the for loop it's
> passed the variable by reference back to $_SESSION['first_name'] and
> $_SESSION['last_name'] as strings;
>
> when it hit's the second iteration on the for loop it now has strings
> to deal with so uses the $key (holding integer 1 at this stage) as a
> string offset thus giving you the second character (offset [1]) of the
> string variables $last/$first. now set to 'cable'/'diane' thus giving
> you the 'a'/'i'. when it does pass three there is no offset [2] so
> gives you nothing.
>
> *phew*
>
> reproduce code!
>
> <?php
>
> $userids = array('6156','1234','8867');
> $first = array('Diane','Big','Joe');
> $last = array('Cable','Ron','Dirt');
>
> function save_to_session( ) {
>  global $userids , $first , $last;
>  $_SESSION['user_id'] = &$userids;
>  $_SESSION['first_name'] = &$first;
>  $_SESSION['last_name']= &$last;
> }
>
> save_to_session( );
>
> print_r( $_SESSION );
>
> $num_users = count($_SESSION['user_id']);
>
> for ($i = 0; $i < $num_users; $i++) {
>     $first = $_SESSION['first_name'][$i];
>     $last = $_SESSION['last_name'][$i];
>     echo "$last, $first\n";
> }
> ?>
>
> Regards!
>

This looks like a bug.  If you are not modifying a variable with the
same name as the session variable, it should not change the session
variable.  Also, why are you assigning references to arrays?  What
version of PHP are you using?  You really should get up to speed on PHP 5.

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux