This is a discussion on Accessing Key=> Value Array by Index within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I hope this is a silly questions (been programming for 4 days straight)... Given an array: $foo = array(one=>...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I hope this is a silly questions (been programming for 4 days straight)... Given an array: $foo = array(one=>11,two=>22,tree=>33); $fum = $foo[0]; $fum returns null. There are cases where I won't know the name "one" and still need to access by Index. HELP!!! |
|
|||
|
Bob Johnson wrote:
> I hope this is a silly questions (been programming for 4 days > straight)... > > Given an array: > $foo = array(one=>11,two=>22,tree=>33); > > $fum = $foo[0]; > > $fum returns null. There are cases where I won't know the name "one" > and still need to access by Index. That's because there is no value in your array with index 0. > HELP!!! Refer to the array_keys() function to find out how to get all the index names of an array: http://www.php.net/array_keys -- Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com |
|
|||
|
"Bob Johnson" <bob@acme.com> kirjoitti
viestissä:300520051442288433%bob@acme.com... > > I hope this is a silly questions (been programming for 4 days > straight)... > > Given an array: > $foo = array(one=>11,two=>22,tree=>33); > > $fum = $foo[0]; > > $fum returns null. There are cases where I won't know the name "one" > and still need to access by Index. > I use the foreach control structure: foreach($foo as $key => $value) { echo "$key contains $value<br>"; } read all about it @ http://www.php.net/foreach -- "I am pro death penalty. That way people learn their lesson for the next time." -- Britney Spears eternal.erectionN0@5P4Mgmail.com |
|
|||
|
Bob Johnson wrote:
> I hope this is a silly questions (been programming for 4 days > straight)... > > Given an array: > $foo = array(one=>11,two=>22,tree=>33); > > $fum = $foo[0]; > > $fum returns null. There are cases where I won't know the name "one" > and still need to access by Index. You can try this: $keys = array_keys($foo); $fum = $foo[$keys[0]]; Berislav |
|
|||
|
In article <d7h5ou$977$1@garrison.globalnet.hr>, Berislav Lopac
<berislav.lopac@lopsica.com> wrote: > Bob Johnson wrote: > > I hope this is a silly questions (been programming for 4 days > > straight)... > > > > Given an array: > > $foo = array(one=>11,two=>22,tree=>33); > > > > $fum = $foo[0]; > > > > $fum returns null. There are cases where I won't know the name "one" > > and still need to access by Index. > > You can try this: > > $keys = array_keys($foo); > $fum = $foo[$keys[0]]; > > Berislav Many thanks - that's perfect. I resorted to using current(), next(), and prev(). This is much cleaner Berislav - appreciate your post! |