Re: Re: SESSION array problems
| From: | Nathan Rixham | Date: | Wed, 01 Oct 2008 22:35:16 +0000 |
| Subject: | Re: Re: SESSION array problems | ||
| References: | 1 2 3 4 | Groups: | php.general php.general |
| Request: | Send a blank email to php-general+get-281353@lists.php.net to get a copy of this message | ||
tedd wrote:
At 3:41 PM -0400 10/1/08, tedd wrote:tedd wrote:My error, if produced. Cable, Diane a, i While print_r($_SESSION); JUST BEFORE IT produced: [user_id] => ArrayWhat about: foreach ($_SESSION['user_id'] as $key => $value) {Jay: Close, it produced: Array, Array Array, Array Array, Array Cheers, tedd$last = $_SESSION['last_name'][$key]; $first = $_SESSION['first_name'][$key]; echo "$last, $first";}( [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
At 3:41 PM -0400 10/1/08, tedd wrote: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++) {My error, if produced. Cable, Diane a, i While print_r($_SESSION); JUST BEFORE IT produced: [user_id] => ArrayWhat about: foreach ($_SESSION['user_id'] as $key => $value) {Jay: Close, it produced: Array, Array Array, Array Array, Array Cheers, tedd$last = $_SESSION['last_name'][$key]; $first = $_SESSION['first_name'][$key]; echo "$last, $first";}( [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
$first = $_SESSION['first_name'][$i];
$last = $_SESSION['last_name'][$i];
echo "$last, $first\n";
}
?>
Regards!
--
nathan ( nathan@kraya.co.uk )
{
Senior Web Developer
php + java + flex + xmpp + xml + ecmascript
web development edinburgh | http://kraya.co.uk/
}