Accessing Key=> Value Array by Index

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=>...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-30-2005
Bob Johnson
 
Posts: n/a
Default Accessing Key=> Value Array by Index


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!!!
Reply With Quote
  #2 (permalink)  
Old 05-30-2005
Chris Hope
 
Posts: n/a
Default Re: Accessing Key=> Value Array by Index

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
Reply With Quote
  #3 (permalink)  
Old 05-30-2005
Kimmo Laine
 
Posts: n/a
Default Re: Accessing Key=> Value Array by Index

"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


Reply With Quote
  #4 (permalink)  
Old 05-31-2005
Berislav Lopac
 
Posts: n/a
Default Re: Accessing Key=> Value Array by Index

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


Reply With Quote
  #5 (permalink)  
Old 06-01-2005
Bob Johnson
 
Posts: n/a
Default Re: Accessing Key=> Value Array by Index

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!
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 11:20 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0