Get the First & Last Item from an Array in PHP
Last Updated :
23 May, 2024
We will get the first and last elements of an array in PHP. This can be particularly useful in scenarios like processing data, working with queues, or implementing specific algorithms.
Below are the approaches to get the first and last items from an array in PHP:
Using Array Indexing
The basic method to access the first and last elements of an array is by using element indexing. The array indexing starts with 0.
Example: In this approach, we directly access the first element using $arr[0] and the last element using $arr[count($arr) - 1].
PHP
<?php
// Get First and Last Array Elements
function getFirstAndLast(&$arr) {
$first = $arr[0];
$last = $arr[count($arr) - 1];
return [$first, $last];
}
// Driver code
$arr = [10, 20, 30, 40, 50];
list($first, $last) = getFirstAndLast($arr);
echo "First Element: $first, Last Element: $last";
?>
OutputFirst Element: 10, Last Element: 50
Using reset() and end() Functions
PHP provides built-in functions reset() and end() to access the first and last elements of an array.
Example: Here, reset($arr) moves the internal pointer to the first element and returns its value, while end($arr) moves the pointer to the last element and returns its value.
PHP
<?php
// Get First and Last Array Elements
function getFirstAndLast(&$arr) {
$first = reset($arr);
$last = end($arr);
return [$first, $last];
}
// Driver code
$arr = [10, 20, 30, 40, 50];
list($first, $last) = getFirstAndLast($arr);
echo "First Element: $first, Last Element: $last";
?>
OutputFirst Element: 10, Last Element: 50
Using array_shift() and array_pop() Function
We can use array_shift() and array_pop() to retrieve and remove the first and last elements of an array, respectively.
Example: The array_shift($arr) removes and returns the first element, and array_pop($arr) removes and returns the last element. Note that this method modifies the original array by removing these elements.
PHP
<?php
// Get First and Last Array Elements
function getFirstAndLast(&$arr) {
$first = array_shift($arr);
$last = array_pop($arr);
return [$first, $last];
}
// Driver code
$arr = [10, 20, 30, 40, 50];
list($first, $last) = getFirstAndLast($arr);
echo "First Element: $first, Last Element: $last";
?>
OutputFirst Element: 10, Last Element: 50
Similar Reads
Find the Most Frequent Element in an Array in PHP Given an array, i.e. $arr, the task is to find the most frequent element in an array. There are multiple ways to solve this problem in PHP we will be going to discuss them. These problems can be used during data analysis, Web Analytics, or highly used in fraud detection. Example: Input: $array = [1,
2 min read
JavaScript - Get first and last Item of JS Array These are the following ways to get first and last element of the given array: 1. Using length PropertyThe array length property in JavaScript is used to set or return the number of elements in an array. JavaScriptlet arr = [3, 2, 3, 4, 5]; let f = arr[0]; let l = arr[arr.length - 1]; console.log(f,
2 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
Find the Second Largest Element in an Array in PHP We will be given with an integer array, i.e. $array1 or $ array2, and the task is to find the second largest element in the array. There are multiple ways to approach and solve this problem however using sorting is the most common and concise approach as we use the rsort() function for further opera
3 min read
PHP Second most frequent element in an array Given an array we have to find the second most frequent element present in it. Examples: Input : array(3, 3, 4, 5, 5, 5, 9, 8, 8, 8, 8, 8); Output : Second most frequent element is: 5 Input : array("geeks", "for", "geeks"); Output : Second most frequent element is: for Here are some common approache
4 min read