Get the First and Last Elements of an 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 …


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen

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 for example 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 for 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 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 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 of last array of an element in PHP. For example you could use array_values() and then getting the first or last element. However, using 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.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen


Print Share Comment Cite Upload Translate Updates
APA

Monty Shokeen | Sciencx (2021-04-19T12:56:17+00:00) Get the First and Last Elements of an Array in PHP. Retrieved from https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/

MLA
" » Get the First and Last Elements of an Array in PHP." Monty Shokeen | Sciencx - Monday April 19, 2021, https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/
HARVARD
Monty Shokeen | Sciencx Monday April 19, 2021 » Get the First and Last Elements of an Array in PHP., viewed ,<https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/>
VANCOUVER
Monty Shokeen | Sciencx - » Get the First and Last Elements of an Array in PHP. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/
CHICAGO
" » Get the First and Last Elements of an Array in PHP." Monty Shokeen | Sciencx - Accessed . https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/
IEEE
" » Get the First and Last Elements of an Array in PHP." Monty Shokeen | Sciencx [Online]. Available: https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/. [Accessed: ]
rf:citation
» Get the First and Last Elements of an Array in PHP | Monty Shokeen | Sciencx | https://www.scien.cx/2021/04/19/get-the-first-and-last-elements-of-an-array-in-php/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.