Re: Array Assignment

From: Date: Fri, 04 May 2001 18:24:21 +0000
Subject: Re: Array Assignment
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51460@lists.php.net to get a copy of this message
On Saturday 05 May 2001 16:55, Mark Cain wrote: > In Perl I can assign dynamic keys ad infinitum to an array such as: > > $sku{$id}{$line}{$price} = 99; Same in PHP: $sku [$id] [$line] [$price] = 99; if $sku is an array. If unsure, initialize it as one: $sku = array (); > Here is the test code: > > $first = "Elementary"; > $second = "Middle"; > $first[$second] = "pass"; > echo "After Assignment:<BR>"; > echo "first = $first<BR>"; // prints: first = plementary --- what > is this ????!! $first is a string, i.e. $first [$n] accesses the $n'th character in the string "Elementary". $second is a string as well. You use it as index in the string, so it is automatically converted to an integer: "Middle" -> 0 So you assign "pass" to the first character of $first. And as you can only assign other single characters to a single char, the first char of "pass" is used. > echo "$first[$second]"; // prints: P That should be a lowercase 'p' (see above) Summary: You thought too complex and thus stumbled over another feature of PHP :) -- Christian Reiniger LGDC Webmaster (http://sunsite.dk/lgdc/) What luck for the rulers that men do not think. - Adolf Hitler

« previous php.general (#51460) next »