Current Location: Home> Latest Articles> How to use end() to determine whether an array contains only one element

How to use end() to determine whether an array contains only one element

M66 2025-06-02

In PHP, array operations are very common, and the end() function is one of the commonly used array functions. It returns the value of the last element of the array. Many times we can use this function to judge some characteristics of an array, especially to determine whether the array has only one element.

What is the end() function?

The end() function points the inner pointer of the array to the last element and returns the value of that element. It should be noted that end() does not change the structure of the array, but only changes the position of the internal pointer.

 $array = [1, 2, 3];
echo end($array); // Output 3

In the above code, end($array) returns the last element 3 of the array.

Use end() to determine whether the array has only one element

We can use the end() function to determine whether an array has only one element. The specific idea is: first use end() to get the last element of the array, then use reset() to reset the pointer of the array to the first element, and then compare these two elements. If the two elements are the same and there are no other elements, then the array has only one element.

Sample code

 <?php
function is_single_element_array($array) {
    // use end() Get the last element of the array
    $lastElement = end($array);

    // use reset() Reset the array pointer to the first element
    $firstElement = reset($array);

    // Determine whether the first element and the last element are the same,And whether the length of the array is 1
    return $lastElement === $firstElement && count($array) === 1;
}

// Test array
$array1 = [5];
$array2 = [1, 2, 3];

echo is_single_element_array($array1) ? 'yes' : '不yes';  // Output yes
echo "\n";
echo is_single_element_array($array2) ? 'yes' : '不yes';  // Output 不yes
?>

Code explanation

  1. end($array) : Gets the last element of the array.

  2. reset($array) : reset the internal pointer of the array to the first element.

  3. count($array) : Returns the number of elements of the array. We use it to make sure there is only one element in the array.

If the last element of the array is the same as the first element and the length of the array is 1, then we can be sure that there is only one element in this array.

Why use end() and reset() ?

The combination of these two functions helps us check if the array has an element without changing the original array structure. end() and reset() operate on the internal pointer of the array, so that we can easily get the first and last elements and compare them.

Things to note

  • end() may change the internal pointer position of the array, so if the array operation depends on the position of the pointer, it is recommended to use reset() after using end() to restore.

  • It should be noted that when the array is empty, end() and reset() both return false , so when judging whether the array has only one element, be sure to ensure that the array is not empty.

Final content