|
-
Jan 21st, 2005, 03:49 PM
#1
Thread Starter
Stuck in the 80s
Printing an element of an Array?
If I define an array like this:
Code:
$arr = array(a => 'apple', b=> 'banana');
How can I access the first element of the array without knowing that the key is a?
echo $arr[?];
Would normally do $arr['a'], but let's say I don't know that the first element's key is a.
Thanks in advance.
-
Jan 21st, 2005, 05:06 PM
#2
Re: Printing an element of an Array?
If you want them back in a particular order then you'll need to use an array sorting function first. Then you can get the first element by:
PHP Code:
reset($arr); // reset just incase array pointer is not at the beginning $firstval = current($arr); // get the first value $firstkey = key($array); // get the first key
-
Jan 21st, 2005, 05:19 PM
#3
Addicted Member
Strange predicament
Why would that ever be a predicament?
foreach($arr as $key => $value){
//Now I can find out the keys and values
break;//If I really only need the 1st element.
}
visualAd is also right.
Circa 1995
Engineer - I think we should put our website address on our paper catalogs.
Vice President - Don't get too excited about this internet thing.
I am sorry, but the Oracle was mistaken. You cannot help us.
-Matrix video game
I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
-Linus
Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.
That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".
Are you threatening me, Master Jedi?
-Chancellor Palpatine
-
Jan 21st, 2005, 05:21 PM
#4
Re: Strange predicament
 Originally Posted by Phenix
Why would that ever be a predicament?
foreach($arr as $key => $value){
//Now I can find out the keys and values
break;//If I really only need the 1st element.
}
visualAd is also right.
If you need all the elements then use foreach, if not it will jsut waste resources.
-
Jan 21st, 2005, 05:37 PM
#5
Thread Starter
Stuck in the 80s
Re: Strange predicament
 Originally Posted by Phenix
Why would that ever be a predicament?
Because I'm sorting the array after operations are done on the values, and I need to pull the first element. After the sorting, I don't know what the key is for the first element anymore.
Thanks, visualAd, that looks like it should work.
-
Jan 21st, 2005, 08:39 PM
#6
Fanatic Member
Re: Printing an element of an Array?
Wouldn't $arr[0] work, or am I missing something here?
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
-
Jan 22nd, 2005, 04:24 AM
#7
Re: Printing an element of an Array?
 Originally Posted by McCain
Wouldn't $arr[0] work, or am I missing something here?
Not if the array is not indexed.
-
Jan 22nd, 2005, 05:11 AM
#8
Fanatic Member
Re: Printing an element of an Array?
Ohh, I thought they allways were. My Bad.
Never argue with fools, they will only drag you down to their level, and beat you with experience.
Q: How do you tell an experienced hacker from a novice?
A: The latter thinks there's 1000 bytes in a kilobyte, while the former is sure there's 1024 meters in a kilometer
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|