Home  >  Article  >  Backend Development  >  How to get the following elements of an array in php

How to get the following elements of an array in php

PHPz
PHPzOriginal
2023-04-18 10:25:20884browse

In PHP development, the use of arrays is very common, and we often need to operate on arrays. One of the common operations is to take the last few elements in the array. This article will introduce several common methods to achieve this goal.

Method 1: Use array_slice() function

array_slice() function can return the specified part from the array. We can get the next few elements in the array by passing the starting index and length parameters. The following is a sample code:

$array = ['apple', 'banana', 'grape', 'orange', 'pear'];
$length = 3;
$startIndex = count($array) - $length;
$lastElements = array_slice($array, $startIndex, $length);
print_r($lastElements);

The running result is as follows:

Array
(
    [2] => grape
    [3] => orange
    [4] => pear
)

In this sample code, we first define an array containing 5 elements. Then, we calculated the number of elements that needed to be obtained, and obtained the subsequent elements by calculating the starting index. Finally, we use the array_slice() function to get the following elements and print the result.

Method 2: Use array_reverse() function

array_reverse() function can reverse the elements in the array. We can reverse the array first and then use the array_slice() function to get the previous elements. The following is a sample code:

$array = ['apple', 'banana', 'grape', 'orange', 'pear'];
$length = 3;
$reversedArray = array_reverse($array);
$lastElements = array_slice($reversedArray, 0, $length);
print_r($lastElements);

The running result is as follows:

Array
(
    [0] => pear
    [1] => orange
    [2] => grape
)

In this sample code, we first define an array containing 5 elements. We then calculated the number of elements we needed to get and reversed the array using the array_reverse() function. Finally, we use the array_slice() function to get the previous element in the reversed array and print the result.

Method 3: Use array_splice() function

array_splice() function can delete or insert elements from the array and return the deleted element. We can use this function to delete the previous elements in the array and return the remaining later elements. The following is a sample code:

$array = ['apple', 'banana', 'grape', 'orange', 'pear'];
$length = 3;
$startIndex = count($array) - $length;
$lastElements = array_splice($array, $startIndex, $length);
print_r($lastElements);

The running result is as follows:

Array
(
    [0] => grape
    [1] => orange
    [2] => pear
)

In this sample code, we first define an array containing 5 elements. Then, we calculated the number of elements that needed to be obtained, and obtained the subsequent elements by calculating the starting index. Finally, we use the array_splice() function to remove the previous elements and return the remaining following elements.

Summary

This article introduces three common methods to get the next few elements in a PHP array. These methods are using the array_slice() function, using the array_reverse() function, and using the array_splice() function. These methods can all help us operate arrays, and you can choose the method that suits you according to the actual situation.

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

Statement:
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