Finding The First And Last Items In An Array In PHP

Getting the first or last item from an array in PHP is usually quite easy. If you create an array and then add a bunch of values to it then the array index will increment by 1 for every element you add. This means that in order to get the first element from an array you just reference the default starting position (0). To get the last item in the array the index key will be the length of the array, minus 1. Here is some example code showing this.

<?php
$array = array();
$array[] = 1;
$array[] = 2;

// get the first item in the array
print $array[0]; // prints 1

// get the last item in the array
print $array[count($array) - 1]; // prints 2

Things become slightly more complicated when the array has non standard key values. Take the following array for example in which the array count it started at 1.

<?php
$array = array(1 => 'one', 2 => 'two', 3 => 'three');

It isn't possible to reference the first item of the array as 0 here because that array key doesn't exist (we would get an 'undefined offset' error in this case). Also, if we try to access the last item in the array in the same way as before then we will return the value 'two' as the length of the array (3) minus 1 will point to the key in the middle of this array.

PHP comes with a couple of functions that can help when pulling out items in an array. One way of doing this is to use the array_values() function, which will return an array with all of the key values reset. This means that the first item in the array will have a key of 0 and the last item will have a key of the length of the array, minus 1. Therefore, you can just use the original method to find the first and last items.

<?php
$array = array(1 => 'one', 2 => 'two', 3 => 'three');

$array_values = array_values($array);

// get the first item in the array
print $array_values[0]; // prints 'one'

// get the last item in the array
print $array_values[count($array_values) - 1]; // prints 'three'

As the array_values() function creates returns a copy of the array we can use the a couple of functions to snip off the beginning and end of the array without altering the original array. The array_shift() function takes an array as it's only parameter and will cut off and return the first item found in that array. The array_pop() function takes an array as it's only parameter and will cut off and return the last item found in the array. This provides us with an easy mechanism to complete our original task without these functions altering the original array.

<?php
$array = array(1 => 'one', 2 => 'two', 3 => 'three');

$array_values = array_values($array);

// get the first item in the array
print array_shift($array_values); // prints 'one'

// get the last item in the array
print array_pop($array_values); // prints 'three'

Another way that is similar to this would to use the array_keys() function to return the first and last keys of the array using array_shift() and array_pop(), which would then be referenced directly within the original array.

<?php
$array = array(1 => 'one', 2 => 'two', 3 => 'three');

$array_keys = array_keys($array);

// get the first item in the array
print $array[array_shift($array_keys)]; // prints 'one'

// get the last item in the array
print $array[array_pop($array_keys)]; // prints 'three'

There are clearly a few ways of doing this simple task in PHP. However, the best way to extract the first and last items from an array is by using the internal PHP array pointer functions. Every array has an internal pointer that points to the 'current' element. When the array is created this pointer is initialised to be the first element in the array. There are a few PHP functions that deal with moving this pointer around an array, but for the purposes of finding the first and last element we only need to look at current(), reset(), and end().

The end() function will move the pointer to the last element in the array and return the value of it. The reset() function will move the pointer to the start of the array and return the first element. The current() function will return the current item in the array, which should be the first element if the array is newly initialised. Using the current() function can be unpredictable unless you know exactly where the array pointer is, in which case the reset() function might be a better choice.

<?php
$array = array(1 => 'one', 2 => 'two', 3 => 'three');

// get the first item in the array (probably)
print current($array); // prints 'one'

// get the first item in the array (definitely)
print reset($array); // prints 'one'

// get the last item in the array
print end($array); // prints 'three'

Using the internal PHP array pointer like this is many times more performant than using a combination of array_values(), array_pop() or count(). This is an important consideration, especially when dealing with multiple arrays in large applications.

Comments

A work mate referred me to your resource. Thank you for the details.
Permalink
this is very good , thanks!
Permalink

It's very article thanks.

Permalink

Thanks for the update Anon. Much appreciated :)

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.