Current Location: Home> Latest Articles> Description of the relationship between end() function and array pointer

Description of the relationship between end() function and array pointer

M66 2025-06-02

When processing arrays in PHP, we often use a set of functions that operate internal array pointers, such as current() , next() , prev() , reset() , key() and end() that we want to explain in depth today. Although these functions may seem simple, their performance when it comes to iterating over data structures is often easily overlooked or even misunderstood. This article will focus on the end() function and analyze its functions, common usage, precautions, and relationships with other pointer functions in depth.

1. Basic usage of end()

end() is a function that points an internal array pointer to the last element of the array and returns the value of that element. The syntax is very simple:

 mixed end(array &$array)

Note that the parameter here is a reference ( &$array ), indicating that end() will directly affect the internal pointer state of the array.

Example:

 $arr = [1, 2, 3, 4];
$last = end($arr);
echo $last; // Output 4

In this code, end($arr) will move the inner pointer of the array to the last element 4 and return it.

2. What is the internal pointer?

PHP's array is not just a collection of key-value pairs, it also maintains an "internal pointer" for tracking the position of the current element when foreach is not explicitly used. The core function of end() is to point this internal pointer to the last element of the array.

In practice, we often do this:

 $arr = ['a' => 100, 'b' => 200, 'c' => 300];

echo current($arr); // 100
next($arr);
echo current($arr); // 200
end($arr);
echo current($arr); // 300

You will find that end() changes the pointer position, and subsequent call to current() becomes pointing to the last element.

3. Comparison with reset(), next(), and prev()

To better understand end() , we compare it with several other functions that operate pointers:

function effect
reset($arr) Move the pointer to the first element
end($arr) Move the pointer to the last element
next($arr) Move the pointer forward by one
prev($arr) Move the pointer one by one
Current($arr) Get the element pointed by the current pointer
key($arr) Get the key name of the element pointed to by the current pointer

By understanding these functions, you can flexibly control the traversal process of the array, not just relying on foreach .

4. The practical application scenarios of end()

1. Get the last element of the array

This is the most typical use:

 $data = ['first', 'second', 'third'];
$lastItem = end($data);
echo $lastItem; // third

2. Determine whether the last item in the array meets a certain condition

 $comments = [
    ['id' => 1, 'user' => 'Tom'],
    ['id' => 2, 'user' => 'Jerry']
];

$last = end($comments);
if ($last['user'] === 'Jerry') {
    echo "The last comment is from Jerry";
}

3. Use key() to get the last key name

 $mapping = ['a' => 1, 'b' => 2, 'c' => 3];
end($mapping);
$lastKey = key($mapping);
echo $lastKey; // Output c

4. Get the url of the last page when processing paging data

 $pages = [
    'https://m66.net/page/1',
    'https://m66.net/page/2',
    'https://m66.net/page/3'
];

$lastPageUrl = end($pages);
echo $lastPageUrl; // https://m66.net/page/3

5. Issues that need to be paid attention to when using end()

1. Passing a reference means that the pointer is changed

If you have performed end() and next() operations on the same array multiple times without reset() , then the current pointer position may not be what you expect, especially when multiplexing array variables in a loop.

2. Calling end() on an empty array will return false

 $empty = [];
var_dump(end($empty)); // bool(false)

This behavior may cause problems when judging types, especially when you don't want false to be confused with array element values, you need to use === to judge the type.

3. Be careful when the array value is false

 $arr = [true, false];
$last = end($arr);
if ($last == false) {
    echo "The last value is false Or does not exist";
}

The above writing method is prone to errors, and congruent judgment should be used:

 if ($last === false) {
    echo "It's indeed false,Not worthless";
}

6. Best Practices

  • Before using end() , make sure you know the position of the current array pointer and call reset() if necessary.

  • Avoid calling end() on an array in foreach , which may cause unexpected side effects.

  • Want to get the last value without affecting the pointer? You can use array_values() or array_key_last() to cooperate with processing:

 $last = array_values($arr)[count($arr) - 1];

or:

 $lastKey = array_key_last($arr);
$last = $arr[$lastKey];

Conclusion

Although the end() function is small, it plays an important role in PHP array operations. Master it and you can more efficiently control the iterative logic of the array and write more flexible and robust code. I hope this article can help you fully understand the essence and usage of end() .