Home > Article > Backend Development > How to delete the first three elements of an array in php
Two methods for PHP to delete the first three elements of an array: 1. Use the array_slice() function to intercept all elements starting from the fourth element (after the third element). The syntax "array_slice($arr ,3)”. 2. Use the array_splice() function to intercept all elements starting from the fourth element (after the third element), the syntax is "array_splice($arr,3)".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php Two ways to delete the first three elements of the array
In PHP, you can use the array_slice() function or array_splice() function to delete the first three elements by intercepting the array.
Method 1. Use array_slice() function
array_slice() function can intercept the specified number (determined by $length parameter) starting from the specified position (determined by $start parameter) , can be omitted.) Elements
If you want to delete the first three elements of the array, just intercept all elements starting from the fourth element (after the third element); that is, set the parameter $start to 3 .
Example: intercept all elements starting from the fourth element
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24,52,90,78); echo "原数组:"; var_dump($arr); echo "删除数组前三个元素:"; $result = array_slice($arr,3); var_dump($result); ?>
##Method 2, use 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 the new array; therefore, the array_splice() function can be used to intercept array fragments. Same as the array_slice() function, just set the second parameter $start of the function to 3.<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24,52,90,78); echo "原数组:"; var_dump($arr); echo "删除数组前三个元素:"; $result = array_splice($arr,3); var_dump($result); ?>
Description: The
array_slice() function is a function provided by PHP to intercept an array. It can 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:
PHP Video Tutorial"
The above is the detailed content of How to delete the first three elements of an array in php. For more information, please follow other related articles on the PHP Chinese website!