php method to remove the first N digits of an array: 1. Use the array_slice() function. You only need to set the second parameter of the function to N. The syntax is "array_slice($arr,N);" ; 2. To use the array_splice() function, just set the second parameter of the function to 0, and set the third parameter start to "N-array length". The syntax is "array_splice($arr,0,(N -array length));".
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php intercepts the last few digits of the array Element
1. Use array_slice() function
array_slice() function is a function provided by PHP to intercept arrays. It can be obtained from Extract a fragment from the array. The syntax is as follows:
array array_slice ( array $arr , int $start [, int $length = NULL [, bool $preserve_keys = false ]] )
Parameter description:
If you want to use the array_slice() function to delete the first N elements of the array, you only need to set the second parameter of the function to N.
Example: Remove the first 5 digits of the array
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24,22,34,56,78,90); echo "原数组:"; var_dump($arr); echo "去掉数组前5位:" ; $result = array_slice($arr,5); var_dump($result); ?>
Output result
2 , use the array_splice() function
When the array_splice() function deletes some elements of the array, it will form these deleted elements into a new array, and then return this new array; therefore array_splice() Functions can be used to intercept array fragments.
Just set the second parameter of the array_splice() function to 0, and set the third parameter start to a negative value (-N) to intercept the last N elements of the array; in other words, the third The first parameter start is set to (N-array length) to remove the first N bits of the array.
Example
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24); echo "原数组:"; var_dump($arr); echo "去掉数组前2位:" ; array_splice($arr,0,(2-5)); var_dump($arr); ?>
Output result
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the first digits of an array in php. For more information, please follow other related articles on the PHP Chinese website!