How to get the last elements of an array in php

PHPz
Release: 2023-04-27 09:42:24
Original
1292 people have browsed it

In PHP, it is a common requirement to get the last few elements of an array. In this article, we will introduce several methods to achieve the function of obtaining the last few elements of an array.

Method 1: Use array_slice()
In PHP, the array_slice() function can obtain the specified part of the array. If the second argument is negative, its value represents the position from the end of the array.

Therefore, we can use the array_slice() function to get the last few elements of the array as follows:

$array = array(1, 2, 3, 4, 5, 6);
$last_elements = array_slice($array, -3);
print_r($last_elements);
Copy after login

This code snippet will output the last three elements of the array:

Array
(
[3] => 4
[4] => 5
[5] => 6
)
Copy after login
Copy after login

Method 2: Use array_splice()
array_splice() function to delete a segment of elements from the array and return the deleted part.

If you do not want to change the original array, you can specify the first parameter as a copied copy array. Something like this:

$array = array(1, 2, 3, 4, 5, 6);
$last_elements = array_splice($array, -3, 3);
print_r($last_elements);
Copy after login

This will return the last three elements and remove them from the array:

Array
(
[0] => 4
[1] => 5
[2] => 6
)
Copy after login

Method Three: Use array_slice() and count()
Another way It is to use the count() function to get the length of the array, and then combine it with the array_slice() function to get the last few elements. For specific implementation, please refer to the following code:

$array = array(1, 2, 3, 4, 5, 6);
$count = count($array);
$last_elements = array_slice($array, $count - 3, 3);
print_r($last_elements);
Copy after login

This will output the following:

Array
(
[3] => 4
[4] => 5
[5] => 6
)
Copy after login
Copy after login

Method 4: Use array_reverse() and array_slice()
A simpler method is to use The array_reverse() function reverses the array and then uses the array_slice() function to get the last few elements.

For specific implementation, please refer to the following code:

$array = array(1, 2, 3, 4, 5, 6);
$reversed_array = array_reverse($array);
$last_elements = array_slice($reversed_array, 0, 3);
print_r($last_elements);
Copy after login

This will output the following:

Array
(
[0] => 6
[1] => 5
[2] => 4
)
Copy after login

Since we use the array_reverse() function to reverse the array, the output result shows is the reverse order of the last three elements.

Summary
Getting the last few elements of an array is often a common requirement in PHP. We can easily implement this functionality by using array_slice(), array_splice(), count() and array_reverse(). No matter which method you use, remember to check the length of the array to avoid out-of-bounds problems.

The above is the detailed content of How to get the last elements of an array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template