Posted by Michael Fesser on 11/02/06 22:48
..oO(Koncept)
>I just ran a small test out of curiosity to see what would happen and I
>must say that I am surprised at the result. Can anybody explain to me
>why this code returns what it does? Does it make sense or is this a
>bug?
>
><?php
>$test['k1']['k2'] = 'hello';
>$test['k1']['k2']['k3'] = 'world';
>
>/*
>Array
>(
> [k1] => Array
> (
> [k2] => wello
> )
>
>)
>*/
>?>
Quite simple:
$test['k1']['k2'] is a string. Now you can access every single char of a
string by simply appending an array-like index, for example:
$string = 'Foo';
print $string[1];
would output 'o'.
In your case you're trying to access the character at position 'k3'.
Casted to an integer 'k3' becomes '0', so you're simply overwriting the
first character of $test['k1']['k2'].
Try this instead:
$test['k1']['k2']['k3'+1] = 'world';
Result? 'hwllo'
Micha
[Back to original message]
|