php method to intercept the last few elements of the array: 1. Use the "array_slice($array,$start)" statement, and the parameter "$start" must be a negative value; 2. Use "array_splice($array, $start)" statement, the parameter "$start" must be a negative value.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
After php intercepts the array How many elements
1. Use array_slice() function
array_slice() function is a function provided by PHP to intercept arrays. A fragment can be extracted from an array. The syntax is as follows:
array array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )
Parameter description:
Example: intercept the last few elements of the array
If you want to intercept the last few elements of the array, just set the parameter start to a negative value
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "截取数组后2位的元素片段:"; $result = array_slice($arr,-2); //截取数组后2位的元素 var_dump($result); ?>
Output result
2. Use the array_splice() function
array_splice() function to delete the array When a part of the elements is removed, these deleted elements will be formed into a new array, and then the new array will be returned; therefore, the array_splice() function can be used to intercept array fragments.
Same as the array_slice() function, you only need to set the second parameter start of the function to a negative value to intercept the last few elements of the array.
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "截取数组后3位的元素片段:"; $result = array_splice($arr,-3); //截取数组后3位的元素 var_dump($result); ?>
Output results
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to intercept the last few digits of an array in php. For more information, please follow other related articles on the PHP Chinese website!