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.
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.
$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.
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.
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 .
This is the most typical use:
$data = ['first', 'second', 'third'];
$lastItem = end($data);
echo $lastItem; // third
$comments = [
['id' => 1, 'user' => 'Tom'],
['id' => 2, 'user' => 'Jerry']
];
$last = end($comments);
if ($last['user'] === 'Jerry') {
echo "The last comment is from Jerry";
}
$mapping = ['a' => 1, 'b' => 2, 'c' => 3];
end($mapping);
$lastKey = key($mapping);
echo $lastKey; // Output c
$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
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.
$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.
$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";
}
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];
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() .