|
thedarkness
|
 |
« on: April 25, 2007, 02:41:30 AM » |
|
Just came across the following syntax and wondered what was the difference is, if any, between this;
$keyboard[$word{$i}]
and this;
$keyboard[$word[$i]]
?
Cheers, td
|
|
|
|
« Last Edit: April 26, 2007, 02:25:50 AM by thedarkness »
|
Logged
|
"I want to be the guy my dog thinks I am." - Unknown
|
|
|
|
Bompa
|
 |
« Reply #1 on: April 25, 2007, 04:23:46 AM » |
|
I'm out of my language, but in perl I would say the first is a hash element and the second is an array element.
Bompa
|
|
|
|
|
Logged
|
"Everything that can be counted does not necessarily count; everything that counts cannot necessarily be counted." -- Albert Einstein
|
|
|
|
perkiset
|
 |
« Reply #2 on: April 25, 2007, 07:22:03 AM » |
|
Did that actually work?
The curlybraces are for surrounding a variable name while dereferencing in a string... I've never seen that notation and I speculate that it is an error... but I'm dying to know if it worked and if so, what it did...
/p
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
perkiset
|
 |
« Reply #3 on: April 25, 2007, 07:24:14 AM » |
|
And BTW - $keyboard[$word[$i]]
... is not actually a multi-dim array at all... its just evaluating the $word array value before evaluating the $keyboard value... multidim would look like this: $keyboard[$x][$y][$z] /p
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
thedarkness
|
 |
« Reply #4 on: April 25, 2007, 04:45:38 PM » |
|
Of course, doh!
Perk: PM'ed you some code.
Cheers, td
|
|
|
|
|
Logged
|
"I want to be the guy my dog thinks I am." - Unknown
|
|
|
|
tobsn
|
 |
« Reply #5 on: May 11, 2007, 12:53:43 PM » |
|
thats for strings $string = '1234567890'; for( $i = 0; $i < strlen( $string ); $i++ ) { echo $string{$i}; }
or it is for generating strings like $i = 1; echo $string{$i}; echo $string1; || $i = 'a'; echo $string{$i}; echo $stringa; cant remember for what it was... one of the examples above 
|
|
|
|
|
Logged
|
No links in signatures please
|
|
|
|
perkiset
|
 |
« Reply #6 on: May 11, 2007, 12:56:34 PM » |
|
simple strings can be referenced with the normal array syntax - that's the only way I've ever done it:
$myStr = 'Hello World'; print $myStr[1]; // prints 'e'
... was this an older PHP syntax?
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
m0nkeymafia
|
 |
« Reply #7 on: May 27, 2007, 01:24:23 PM » |
|
td:
I think strings are stored as a character array, so $charArray[$i] will return the character at that index.
|
|
|
|
|
Logged
|
I am Tyler Durden
|
|
|
|
perkiset
|
 |
« Reply #8 on: May 27, 2007, 02:08:23 PM » |
|
They are... the only caveat is that this is a read only feature... unlike C you cannot write to those array positions.
Which is a drag, really...
/p
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|
m0nkeymafia
|
 |
« Reply #9 on: May 28, 2007, 09:00:16 AM » |
|
Didnt know that perk, bet that caused a few people a few headaches in the past
|
|
|
|
|
Logged
|
I am Tyler Durden
|
|
|
|
thedarkness
|
 |
« Reply #10 on: May 28, 2007, 06:28:18 PM » |
|
td:
I think strings are stored as a character array, so $charArray[$i] will return the character at that index.
Yeah, I truly grok this dude. The OP was about the use of the unusual syntax, not "Can anyone teahc me how to use character arrays?"  Cheers, td
|
|
|
|
|
Logged
|
"I want to be the guy my dog thinks I am." - Unknown
|
|
|
|
perkiset
|
 |
« Reply #11 on: May 28, 2007, 07:59:47 PM » |
|
Didnt know that perk, bet that caused a few people a few headaches in the past
Quick headache, since it is painfully obvious you can't write to the array (it'll bomb at run time) but an enormous tease... it looks SO CLOSE with the read syntax... pissed me off. I'd love direct access to that memory but doing so would open a can of worms as huge as C++ itself. The next thing you know it's pointers to pointers and self-managed garbage collection. Ah well... cest la vie... /p
|
|
|
|
|
Logged
|
It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
|
|
|
|