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 Steve on 11/02/06 22:55

| > you are forcing to treat the string "hello" as an array...and php is
mixing up the
| > hash for k2 when retrieving it as a result.
|
| Labelled as "poor programming" or not, I am only interested in
| understanding why this is happening based on the code provided.

as i tried to explain...and the way it was programmed goes to cause...the
"understanding why".

| If I were forcing to treat the string "hello" as an array, wouldn't php
| be kind enough to automagically create an index of 0 for this value in
| [k1][k2], or at the least, throw up a nice juicy warning telling me
| that I'm an idiot for trying to kick it in the nuts?

no, what you have is a string being forced to be treated natively as an
array and assigned to the K2 key. you also have an actual array assigned to
the K2 key after that. that forces "hello" to be seen as array('h', 'e',
'l', 'l', 'o') ... rather than just [K2] = "hello".

as for the nice, juicy warning...had i programmed that portion of php, i'd
have certainly said something like, "dumbass". as it is, i think you can
tell why i wasn't asked to. ;^)

| $test[] = "hello"; // would create an automatic index of 0 for the
| value "hello"

yes, but that is FAR from your example of [K2] = "hello". and if you did
this:

[K2][] = "hello";

followed by:

[K2][K3] = "world";

php would indeed give you your nice, juicy error.

| What I expected (at the very least) is that k2 would be preserved as
| "hello" at index 0, while all other keys and values of the $test hash
| would be ignored. How come the first character of k3's value is
| maintained, yet at the same time, overwritten with characters one
| onwards from string k2?

then you're expecting the wrong thing. (at the very least) you should expect
[K2] to equal an array where the key is "h" and the value assigned = "ello".
literally, this IS what is going on. the preservation of "ello" and the
prefixing of it with a "w" is a side effect because of how php is
interpreting your arrays...one, an array of characters, the other, an array
element being assigned a string value.

i'm not going to go much further than that in explaining this. google hash
tables and associative arrays. this is not just a php thing...javascript
handles some aspects of associative arrays in the same manner. anyway, hope
that helps you get started.

| I still don't follow the process. How *exactly* is PHP mixing up the
| process?

hope that helps explain it a little more.

| --
| Koncept <<
| "The snake that cannot shed its skin perishes. So do the spirits who are
| prevented from changing their opinions; they cease to be a
pirit." -Nietzsche

"The flower who lost it's stamen must no longer be a flower; just as the
cameleon who is prevented from changing its color must no longer be a
cameleon." - spiritual apple

 

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

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