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.
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
Re: Printing an element of an Array?
Wouldn't $arr[0] work, or am I missing something here?
Re: Printing an element of an Array?
Quote:
Originally Posted by McCain
Wouldn't $arr[0] work, or am I missing something here?
Not if the array is not indexed.
Re: Printing an element of an Array?
Ohh, I thought they allways were. My Bad.