Logo

How to Get First and Last Element of Array in PHP?

How to Get First and Last Element of Array in PHP?

How to Get First and Last Element of Array in PHP?

In this detailed guide, we will cover how to get the first and last elements of an array in PHP. Understanding how to manipulate arrays is fundamental for any PHP developer. This tutorial will provide multiple examples to demonstrate different methods for retrieving the first and last elements of an array.

Example 1 : Using reset() and end()​

In this example, reset() is used to move the internal pointer to the beginning of the array and return the first element, while end() is used to move the internal pointer to the end of the array and return the last element.

				
					/* Sample array */
$myArray = array("apple", "banana", "orange", "grape", "kiwi");

/* Get the first element */
$firstElement = reset($myArray);

/* Get the last element */
$lastElement = end($myArray);

/* Output the results */
echo "First element: " . $firstElement . "\n";
echo "Last element: " . $lastElement . "\n";
				
			

Output:

				
					First element: apple
Last element: kiwi
				
			

Example 2: Using Array Indexing​

Another simple method to get the first and last elements of an array is by using array indexing.

				
					/* Sample array */
$myArray = array("apple", "banana", "orange", "grape", "kiwi");

/* Get the first element using array indexing */
$firstElement = $myArray[0];

/* Get the last element using array indexing */
$lastElement = $myArray[count($myArray) - 1];

/* Output the results */
echo "First element: " . $firstElement . "\n";
echo "Last element: " . $lastElement . "\n";
				
			

Output:

				
					First element: apple
Last element: kiwi
				
			

Example 3: Using array_shift() and array_pop()

array_shift() removes and returns the first element of an array, while array_pop() removes and returns the last element of an array. These functions modify the original array.

				
					/* Sample array */
$myArray = array("apple", "banana", "orange", "grape", "kiwi");

/* Get the first element using array_shift */
$firstElement = array_shift($myArray);

/* Get the last element using array_pop */
$lastElement = array_pop($myArray);

/* Output the results */
echo "First element: " . $firstElement . "\n";
echo "Last element: " . $lastElement . "\n";
				
			

Output:

				
					First element: apple
Last element: kiwi
				
			

Example 4: Using Array Keys

In associative arrays, we can get the first and last elements using the keys.

				
					/* Sample associative array */
$myArray = array("first" => "apple", "second" => "banana", "third" => "orange", "fourth" => "grape", "fifth" => "kiwi");

/* Get the first element */
$firstElement = reset($myArray);

/* Get the last element */
$lastElement = end($myArray);

/* Output the results */
echo "First element: " . $firstElement . "\n";
echo "Last element: " . $lastElement . "\n";
				
			

Output:

				
					First element: apple
Last element: kiwi
				
			

Example 5: Using Array Destructuring (PHP 7.1+)

Array destructuring can also be used to get the first and last elements of an array.

				
					/* Sample array */
$myArray = ["apple", "banana", "orange", "grape", "kiwi"];

/* Get the first element */
[$firstElement] = $myArray;

/* Get the last element */
[$lastElement] = array_slice($myArray, -1);

/* Output the results */
echo "First element: " . $firstElement . "\n";
echo "Last element: " . $lastElement . "\n";
				
			

Output:

				
					First element: apple
Last element: kiwi
				
			

Conclusion :

This tutorial has covered multiple methods to get the first and last elements of an array in PHP. Whether you use reset() and end(), array indexing, array_shift() and array_pop(), or array destructuring, each method has its use cases and benefits. By mastering these techniques, you can efficiently manipulate arrays in your PHP applications.

Scroll to Top