Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. PHP

Get the First and Last Elements of an Array in PHP

by
Difficulty:BeginnerLength:ShortLanguages:

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!

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:

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.

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.

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.

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.

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.

Advertisement
Advertisement
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.