Home >Backend Development >PHP Problem >How to remove the first two items of an array in php
php method to remove the first two items of the array: 1. Use the array_splice() function to intercept all elements of the array starting from subscript 2. The syntax is "array_splice($arr,2)"; 2. Use array_slice() function, syntax "array_slice($arr,2)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php before removing the array Two items
Method 1: Use the array_splice() function
When the array_splice() function deletes some elements of the array, these will be The deleted elements are formed into a new array, and the new array is returned; therefore the array_splice() function can be used to intercept array fragments.
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24,22); echo "原数组:"; var_dump($arr); echo "去掉数组前2项后的元素片段:"; $result = array_splice($arr,2); //去掉数组前2位的元素 var_dump($result); ?>
Method 2: Use array_slice() function
array_slice() function is one provided by PHP to intercept arrays Function that extracts a fragment from an array.
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(10,12,20,25,24,22); echo "原数组:"; var_dump($arr); echo "去掉数组前2项后的元素片段:"; $result = array_slice($arr,2); //截取数组前2位的元素 var_dump($result); ?>
Recommended learning: "PHP Video Tutorial", "PHP ARRAY"
The above is the detailed content of How to remove the first two items of an array in php. For more information, please follow other related articles on the PHP Chinese website!