RE: [PHP] Array Assignment

From: Date: Fri, 04 May 2001 16:14:36 +0000
Subject: RE: [PHP] Array Assignment
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-51435@lists.php.net to get a copy of this message
"Mark Cain" <markcain@home.com> wrote in message news:9cufuv$s20$1@toye.p.sourceforge.net... > > 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 = $first<BR>"; // prints: first = Elementary > echo "second = $second<P>"; // 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 = $first<BR>"; // prints: first = plementary --- what is > this ????!! > echo "second = $second<BR>"; // 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.

« previous php.general (#51435) next »