How can get first and last value in array in php?

It is very common when writing code that you will have to deal with a collection of objects. It could be a set of customer names or the number of posts created by different users. Storing this data in arrays helps us to work with the whole collection by iterating through it one by one.

PHP comes with two different types of arrays to help you store data. You can either use simple numerical arrays or you can create associative arrays. Numerical arrays are helpful when you want to just store a list of items—for example a list of customers. Associative arrays are useful when you want to store key-value pairs like a list of customer IDs and the total value of products purchased by each one.

Every now and then, you will have to access directly access the elements in the arrays you created. In this quick tip, I'll show you how to get the first or last element of an array in PHP.

Get the First Element of an Array in PHP

It's easy to get the first element of a simple numerical array. Just get the element with index 0!

<?php

$names = ["Adam", "Monty", "Sajal", "Andrew"];

// Output — Adam
echo $names[0];

?>

However, this doesn't work for an associative array.

One of the most efficient ways of getting the first element of a numerical or associative array in PHP is to use the reset() function. This function sets the internal pointer of the array to its first element and returns the value of the first element. Here are some examples:

<?php

$names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy'];

$country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra'];

// Output — Andrew
echo reset($names);

// Output — Ottawa
echo reset($country_capitals);

?>

As you can see, we did not need to know the key for the first element to get its value. All we have to do is pass the array to reset().

As I mentioned earlier, using reset() will change the internal pointer of the array. If you want to get the first element from the array without making any changes to it, you can use the array_key_first() function. This will give you the first key of the array, which can be used to get the value of the first element.

<?php

$names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy'];

$country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra'];

// Output — Andrew
echo $names[array_key_first($names)];

// Output — Ottawa
echo $country_capitals[array_key_first($country_capitals)];

?>

One thing you should keep in mind is that array_key_first() is only available starting from PHP 7.3.

Get the Last Element of an Array in PHP

For a simple numerical array, you can get the last element by calculating its index from the array length.

<?php

$names = ["Adam", "Monty", "Sajal", "Andrew"];

// Output — Andrew
echo $names[count($names)-1];

?>

However, once again, this will not work with associative arrays.

You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value. This makes it similar to the reset() function we discussed in the previous section.

<?php

$names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy'];

$country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra'];

// Output — Roy
echo end($names);

// Output — Canberra
echo end($country_capitals);

?>

Just like array_key_first(), there is also a corresponding array_key_last() function which gives you the last key of the array without modifying it in any way. You can easily access the last element of the array once you have the key.

<?php

$names = ['Andrew', 'Adam', 'Monty', 'James', 'Amanda', 'Jessica', 'Roy'];

$country_capitals = ['Canada' => 'Ottawa', 'Germany' => 'Berlin', 'United States' => 'Washington D.C.', 'China' => 'Beijing', 'India' => 'New Delhi', 'Australia' => 'Canberra'];

// Output — Roy
echo $names[array_key_last($names)];

// Output — Canberra
echo $country_capitals[array_key_last($country_capitals)];

?>

The array_key_last() function is also available starting from PHP 7.3.

Final Thoughts

There are many more methods to get the first or last array of an element in PHP. For example, you could use array_values() and then get the first or last element. However, using the reset(), array_key_first(), end(), and array_key_last() functions would be more efficient if you don't want any extra information from the array.

Did you find this post useful?

How can get first and last value in array in php?

Freelancer, Instructor

I am a full-stack developer who also loves to write tutorials. After trying out a bunch of things till second year of college, I decided to work on my web development skills. Starting with just HTML and CSS, I kept moving forward and gained experience in PHP, JavaScript and Python. I usually spend my free time either working on some side projects or traveling around.

How do you find the first and last value of an array?

To get the first and last elements of an array, access the array at index 0 and the last index. For example, arr[0] returns the first element, whereas arr[arr. length - 1] returns the last element of the array.

How do you print the first and last element of an array in PHP?

$firstEle = reset($arr); reset() rewinds array's internal pointer to the first element and returns the value of the first array element. And end() to get the last: $lastEle = end($arr);

How can we get the last element of an array in PHP?

You can use the end() function in PHP to get the last element of any PHP array. It will set the internal pointer to the last element of the array and return its value.

How can we get the first element of an array in PHP?

There are several methods to get the first element of an array in PHP. Some of the methods are using foreach loop, reset function, array_slice function, array_values, array_reverse, and many more.