Re: [PHP] Array Assignment
Mark Cain [EMAIL PROTECTED] wrote in message 9cufuv$s20$[EMAIL PROTECTED]">news:9cufuv$s20$[EMAIL PROTECTED]... Two things: 1) Why does the code below produce this result? 2) How do I assign to an multidimensional array with dynamic keys? Here is the test code: $first = Elementary; $second = Middle; echo before assignment:BR; echo first = $firstBR;// prints: first = Elementary echo second = $secondP; // prints: second = Middle --- looks as expected here look, before the assignment: 0. both $first and $second are string-type variables. $first[$second] = pass; 1. In this case the brackets mean to index a character of the string (like in C), but 2. $second is converted into int because of using as a string-index, the integer value of $second (Middle) equals to 0. (how can you convert it in any other way?) 3. Things getting clearer... the your assignment says: - the 1st character of string $first let be pass, there is no room any other character, but p. (because of PHP think you'd like to change the 1st char only.) 4. so: Elementary --- plementary echo After Assignment:BR; echo first = $firstBR;// prints: first = plementary --- what is this !! echo second = $secondBR; // prints: second = Middle echo $first[$second];// prints: P what you want is the following - i think (i'm not familiar with perl at all): $first = array($first, $second); what this produces is can be tested with var_dump($first) not with echo, by the way var_dump($first); This is a one-dimensional array with two entries. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Array Assignment
So sprach Christian Reiniger am Fri, May 04, 2001 at 08:24:21PM +0200: On Saturday 05 May 2001 16:55, Mark Cain wrote: $first = Elementary; $second = Middle; $first[$second] = pass; echo After Assignment:BR; echo first = $firstBR;// prints: first = plementary --- what is this !! $first is a string, i.e. $first [$n] accesses the $n'th character in the string Elementary. Well, I think the issue is, that Mark first assigned something to $first, which then made this a string variable. Later on he assigned something to $first[$second]. This broke because of the reasons Christian mentioned. Mark, if you directly assign $first[$second] the value pass, then it should work. Alexander Skwar -- How to quote: http://learn.to/quote (german) http://quote.6x.to (english) Homepage: http://www.digitalprojects.com | http://www.iso-top.de iso-top.de - Die günstige Art an Linux Distributionen zu kommen Uptime: 21 hours 11 minutes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP] Array Assignment
Mark Cain [EMAIL PROTECTED] wrote in message 9cufuv$s20$[EMAIL PROTECTED]">news:9cufuv$s20$[EMAIL PROTECTED]... Two things: 1) Why does the code below produce this result? 2) How do I assign to an multidimensional array with dynamic keys? Here is the test code: $first = Elementary; $second = Middle; echo before assignment:BR; echo first = $firstBR;// prints: first = Elementary echo second = $secondP; // prints: second = Middle --- looks as expected here look, before the assignment: 0. both $first and $second are string-type variables. $first[$second] = pass; 1. In this case the brackets mean to index a character of the string (like in C), but 2. $second is converted into int because of using as a string-index, the integer value of $second (Middle) equals to 0. (how can you convert it in any other way?) 3. Things getting clearer... the your assignment says: - the 1st character of string $first let be pass, there is no room any other character, but p. (because of PHP think you'd like to change the 1st char only.) 4. so: Elementary --- plementary echo After Assignment:BR; echo first = $firstBR;// prints: first = plementary --- what is this !! echo second = $secondBR; // prints: second = Middle echo $first[$second];// prints: P what you want is the following - i think (i'm not familiar with perl at all): $first = array($first, $second); what this produces is can be tested with var_dump($first) not with echo, by the way var_dump($first); This is a one-dimensional array with two entries. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Array Assignment
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 = $firstBR;// 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]