Current Location: Home> Latest Articles> Use end() to combine array_slice() to extract several values ​​at the end of the array

Use end() to combine array_slice() to extract several values ​​at the end of the array

M66 2025-06-02

In PHP, when processing arrays, you often encounter situations where you need to extract the last few elements of the array. Today, we will discuss two commonly used methods: the end() function and the array_slice() function.

1. Use the end() function to get the last element of the array

The end() function is used to move the inner pointer of the array to the last element and return the value of that element. This method is suitable for getting the last element of the array. If you only need to access the last element of the array, end() is a very concise way.

 <?php
$array = [1, 2, 3, 4, 5];
$lastElement = end($array); // Get the last element of the array
echo "The last element of the array is: " . $lastElement; // Output: 5
?>

As shown above, end($array) points the array pointer to the last element and returns that element. It should be noted that end() does not modify the original array, it only affects the position of the internal pointer.

2. Use array_slice() function to extract the last few elements of the array

If we need to extract the last few elements of the array, not just the last element, the array_slice() function is very useful. The array_slice() function can extract slices of a specified length from an array. To get the last few elements, we just need to use negative numbers as offsets.

 <?php
$array = [1, 2, 3, 4, 5];
$lastThreeElements = array_slice($array, -3); // Get the last three elements
print_r($lastThreeElements); // Output: Array ( [0] => 3 [1] => 4 [2] => 5 )
?>

In this example, array_slice($array, -3) means to extract to the end of the array starting from the third last element of the array $array . array_slice() also allows us to specify the length of the slice, and if not specified, it will be extracted to the end of the array by default.

3. Use end() and array_slice() to extract different numbers of elements

Sometimes we may need to get the last element and the last few elements at the same time. By combining end() and array_slice() , we can flexibly handle the end of the array.

 <?php
$array = [1, 2, 3, 4, 5];
$lastElement = end($array); // Get the last element
$lastTwoElements = array_slice($array, -2); // Get the last two elements

echo "The last element is: " . $lastElement . "\n"; // Output: 5
print_r($lastTwoElements); // Output: Array ( [0] => 4 [1] => 5 )
?>

4. URL replacement in actual application

In actual development, the URL may be processed and its domain name needs to be modified. For example, suppose we include a URL when processing certain data, we can dynamically modify it by replacing the URL's domain name.

Assuming the original URL is http://example.com/path/to/resource , we need to replace its domain name with m66.net . Here is an example of how to do this: