2

Possible Duplicate:
Get first element of an array

What is the fastest and easiest way to get the first item of an array in php? I only need the first item of the array saved in a string and the array must not be modified.

2
  • 1
    An array saved in a string? Could you give an example of that array? Commented Oct 8, 2012 at 10:39
  • @Ikke: I think he meant he wants the first item of the array to be saved in a string. Arrays cannot be saved in strings. Commented Oct 8, 2012 at 10:42

6 Answers 6

4

I'd say that this is very optimized:

echo reset($arr); 
Sign up to request clarification or add additional context in comments.

Comments

2

I could not but try this out

$max = 2000; $array = range(1, 2000); echo "<pre>"; $start = microtime(true); for($i = 0; $i < $max; $i ++) { $item = current($array); } echo microtime(true) - $start ,PHP_EOL; $start = microtime(true); for($i = 0; $i < $max; $i ++) { $item = reset($array); } echo microtime(true) - $start ,PHP_EOL; $start = microtime(true); for($i = 0; $i < $max; $i ++) { $item = $array[0]; } echo microtime(true) - $start ,PHP_EOL; $start = microtime(true); for($i = 0; $i < $max; $i ++) { $item = &$array[0]; } echo microtime(true) - $start ,PHP_EOL; $start = microtime(true); for($i = 0; $i < $max; $i ++) { $item = array_shift($array); } echo microtime(true) - $start ,PHP_EOL; 

Output

0.03761100769043 0.037437915802002 0.00060200691223145 <--- 2nd Position 0.00056600570678711 <--- 1st Position 0.068138122558594 

So the fastest is

 $item = &$array[0]; 

3 Comments

But he wants a string, not a reference.
@Lightness Races in Orbit the reference was just for a benchmark
Although not always first element would be under [0]
1

Use reset:

<?php $array = Array(0 => "hello", "w" => "orld"); echo reset($array); // Output: "hello" ?> 

Note that the array's cursor is set to the beginning of the array when you use this.

Live demonstration

(Naturally, you can store the result into a string instead of echoing, but I use echo for demonstration purposes.)

4 Comments

I'm not aware of such function.
@Veseliq: uk.php.net/reset -- now you are!
You suggested begin() before the edit :)
@Veseliq: What edit?! You are hallucinating...! ;)
0

Something like this?:

$firstitem = $array[0]; 

4 Comments

Only works with numeric keys.
Only works with numeric keys that are in numeric order.
True, but he did ask for the fastest :P
The fastest way to fail, perhaps
0

reset does this:

$item = reset($array); 

This will work irrespective of what the keys are, but it will move the array pointer (I 've never had a reason to worry about this, but it should be mentioned).

Comments

0

The most efficient is getting the reference, so not string copy is involved:

$first = &$array[0]; 

Just make sure you don't modify $first, as it will be modified in the array too. If you have to modify it then look for the other answers alternatives.

3 Comments

But this doesn't store anything into a string.
@LightnessRacesinOrbit It just stores the reference, not the contents, that's the reason it's the most efficient.
It's also the reason that it does not answer the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.