Home Backend Development PHP Problem How to get the last elements of an array in php

How to get the last elements of an array in php

Apr 27, 2023 am 09:11 AM

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)