You are here: Re: Why does this array result occur? « All PHP « IT news, forums, messages
Re: Why does this array result occur?

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация