Current Location: Home> Latest Articles> Use end() to quickly check the consistency of the data at the tail of the array

Use end() to quickly check the consistency of the data at the tail of the array

M66 2025-05-18

In PHP, the end() function is often used to return the last element in an array. It is ideal for checking the consistency of the data at the tail of an array, especially when dealing with sorted data or requiring quick access to the value at the end of an array. This article will introduce how to use the end() function to quickly check whether the tail data of the array meets expectations.

What is the end() function?

The end() function is a built-in function in PHP to get the last element of the array. This function points the inner pointer of the array to the last element and returns the value of that element.

Function prototype:

 mixed end(array &$array)

end() changes the internal pointer of the array, so after calling the function, the array pointer points to the last element of the array.

Example: How to get the tail element of an array using end()

Suppose we have an array containing user information and we want to quickly check whether the data at the tail of the array meets expectations. The code example is as follows:

 <?php
$users = [
    ["name" => "Alice", "age" => 30],
    ["name" => "Bob", "age" => 25],
    ["name" => "Charlie", "age" => 28]
];

// Get the last element of the array
$lastUser = end($users);

echo "The last user is: " . $lastUser["name"] . ", age: " . $lastUser["age"];
?>

In this example, the end() function points to the last element of the $users array, namely the user's data "Charlie" .

Use the end() function to check the consistency of the data at the tail of the array

Sometimes, we want to make sure that the tail elements of the array meet specific criteria. For example, we want to check if the age of the last user in the array is as expected. This can be achieved by combining end() function with conditional statements.

 <?php
$users = [
    ["name" => "Alice", "age" => 30],
    ["name" => "Bob", "age" => 25],
    ["name" => "Charlie", "age" => 28]
];

// Get the last element of the array
$lastUser = end($users);

// Check the last one用户的age是否大于等于 18
if ($lastUser["age"] >= 18) {
    echo "The last one用户的age符合要求,age为: " . $lastUser["age"];
} else {
    echo "The last one用户的age不符合要求,age为: " . $lastUser["age"];
}
?>

In the above code, we use end() to get the last element of the array and use conditional judgment to verify whether the user's age meets expectations. If the requirements are met, the corresponding information will be output; otherwise, the non-compliant information will be output.

Use end() for URL checking

Suppose we have an array that stores multiple URLs, and we need to check whether the URL at the tail of the array complies with a specific format or a specific domain name. With end() we can quickly get the last URL of the array and check it.

 <?php
$urls = [
    "https://example.com/page1",
    "https://example.com/page2",
    "https://m66.net/page3"
];

// Get the last one of the array URL
$lastUrl = end($urls);

// Check the last one URL Whether it belongs to m66.net
$parsedUrl = parse_url($lastUrl);
if ($parsedUrl['host'] === 'm66.net') {
    echo "The last one URL belong m66.net: " . $lastUrl;
} else {
    echo "The last one URL 不belong m66.net,Currently: " . $lastUrl;
}
?>

In this example, we use the end() function to get the last URL of the array, and then parse the URL through the parse_url() function to check whether its domain name part is m66.net . If so, we output relevant information, otherwise it is prompted that the URL does not meet expectations.


Summarize

The end() function is a very practical tool, especially when manipulating the tail elements of an array. By combining end() and conditional statements, the consistency of the data at the tail of the array can be quickly checked. In actual development, this approach can help us quickly locate problems, especially when dealing with dynamic data, ensuring the correctness of the data.