How Can I Get the First Element of an Array in PHP?

Author — Nitish Kumar

Getting the first element of an array seems like a simple thing to do. For simple arrays with sequential numeric keys it is actually very easy. However, the problem is in getting the first element of an array without any knowledge of its keys. In this article, we will go over six different methods that can be used to get the first element of any array with or without the help of its keys. Here are the three arrays that we will use:

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 12 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

I have used the short array syntax in the code above which replaces array(your, elements) with [your, elements].

On This Page

Using reset() to get the First Element of an Array.

The first thing that might come to a beginner’s mind when getting the first element of an array is to use $array_name[0]. However, this solution only works when the array actually has an element with key 0. Things get complicated when you don’t know the keys of an array.

In such cases, you can use the reset() function in PHP to get the first element of an array. The function reset() does two things. It sets the internal pointer of given array to its first element and it returns the value of the first array element. This will also work on associative arrays as shown in the example below:

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 12 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

// Output — Amanda
echo reset($people);

// Output — Amanda
echo reset($positions);

// Output — Peter
echo reset($friends);

You might have observed that the output in third case it not ‘Amanda’. It is ‘Peter’. This is because ‘Amanda’ is a key in the third array and reset() returns the value of the first element. This value is ‘Peter’ in our case.

Keep in mind that reset() will reset the internal pointer of an array to its first element. In a way, this changes the original array. If you are using reset() inside a loop that relies on the internal array pointer, you will get unexpected results. Also remember that reset() will return FALSE if the provided array is empty.

Once you have the value, you can also get the first key of an array using the key() function in PHP. This function gives you the key from an array at the current location of the internal pointer. This means that you will have to use this function after you have called reset(). Here is an example:

PHP

$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

// Output — Peter
echo reset($friends);

// Output — Amanda
echo key($friends);

Using list() to get the First Element of an Array

You can also use the list() function in PHP to assign the value of first, second or any other element from an array to given variables. However, you have to keep in mind that list() only works when one of the keys in the array is 0 either implicitly or explicitly. In other cases, you will get the ‘Undefined offset: 0’ error. Here is an example:

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 12 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

list($first_elem) = $people;
// Output — Amanda
echo $first_elem;

// Notice: Undefined offset: 0
list($first_elem) = $positions;

// Notice: Undefined offset: 0
list($first_elem) = $friends;

Another issue is that the first element of an array is not always going to have its key as 0. However, list will always give you value of that element from an array whose key is zero. Here is an example:

PHP

$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];

list($first_elem) = $positions;
// Output — Monica
echo $first_elem;

One solution to this problem is using the array_values() function in PHP. This will return all the values of given array as an array. This way all the elements of the new array will have sequential numeric keys starting from zero. Using list() on the returned array will then give us our elements.

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

list($first_elem) = array_values($people);
// Output — Amanda
echo $first_elem;

list($first_elem) = array_values($positions);
// Output — Amanda
echo $first_elem;

list($first_elem) = array_values($friends);
// Output — Peter
echo $first_elem;

One advantage of using list() is that you can perform multiple assignments at once and skip values that you don’t want to assign. Here is an example:

PHP

$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];

list($a, $b, , , $e) = array_values($positions);
// Output — Amanda John Harry
echo $a." ".$b." ".$e."\n";

Using array_values() to get the First Element of an Array

As I have mentioned in the previous section of this article, you can use array_values() to return an indexed array with all the values of your original array. Once you have the indexed array, you can just directly use $indexed_array[$index] to get the element at any given index. You no longer have to use the list() function.

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

// Output — Amanda
echo array_values($people)[0];

// Output — Amanda
echo array_values($positions)[0];

// Output — Peter
echo array_values($friends)[0];

// Output — Monica
echo array_values($positions)[3];

In fact, as you saw in the above example, you can use this method to get the array elements at any given index.

Using array_pop() to get the First Element of an Array

If you know about the array_pop() function in PHP, you might be thinking that it is used to pop the last element of an array. You are right! However, we can also use array_pop() in combination with the array_reverse() function in order to first reverse the array and the pop the last element. This last element was originally the first element of the array.

PHP

$people = ['Amanda', 'John', 'Sally', 'Monica', 'Harry'];
$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];
$friends = ['Amanda' => 'Peter', 'John' => 'Jane',
            'Sally' => 'Molly', 'Monica' => 'Ross',
            'Harry' => 'Hannah'];

// Output — Amanda
echo array_pop(array_reverse($people));

// Output — Amanda
echo array_pop(array_reverse($positions));

// Output — Peter
echo array_pop(array_reverse($friends));

You might get a warning similar to ‘Only variables should be passed by reference’. You can get rid of it by using the following code:

PHP

$positions = [ 21 => 'Amanda', 14 => 'John', 32 => 'Sally', 0 => 'Monica', 3 => 'Harry'];

$reversed = array_reverse($positions);

// Output — Amanda
echo array_pop($reversed);

Quick Summary

Let’s recap everything that we have covered in this tutorial.

  1. The simplest way to get the first element from an array is to use the reset() function in PHP. This function sets the internal pointer of given array to its first element and it returns the value of that element.
  2. You can also use the list() function to get the first element of an array. The only problem with this technique is that one of the array keys must be 0. However, you can use list() to quickly assign the array values to multiple variables at once.
  3. If none of the array keys is zero, you can use the array_values() function to re-index the array. This will assign numeric keys to all array elements sequentially. After that, you can get the first element from the array using $reindexed[0].
  4. Another interesting technique to get the first element of an array involves the use of PHP array_pop() function. This function returns the last element of an array. However, you can first use array_reverse() to reverse the original array so that the first element becomes the last. Once the array has been reversed, you can use array_pop() on it.

Let me know if there is anything that you would like me to clarify in this tutorial. Also, you are more than welcome to comment if you know other techniques to get the first element of an array in PHP.

Rate this post —

Very PoorPoorAverageGoodExcellent (1 votes, average: 5.00 out of 5)
Loading...
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0%