The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 19, 2019, 05:42:54 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: Array index syntax  (Read 4719 times)
thedarkness
Lifer
*****
Offline Offline

Posts: 585



View Profile
« 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
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« 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

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« 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
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« 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
Lifer
*****
Offline Offline

Posts: 585



View Profile
« 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
n00b
*
Offline Offline

Posts: 3


View Profile
« Reply #5 on: May 11, 2007, 12:53:43 PM »

thats for strings

Code:
$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
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« 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
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« 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
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« 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
Expert
****
Offline Offline

Posts: 240


Check it!


View Profile
« 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
Lifer
*****
Offline Offline

Posts: 585



View Profile
« 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?"  Wink

Cheers,
td
Logged

"I want to be the guy my dog thinks I am."
 - Unknown
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« 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.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!