Home >Backend Development >PHP Problem >How to remove partial values from an array in php
Two methods: 1. Use "array_splice($arr, position, number)" to delete the specified number of values starting from the specified position. If the number of deletions is not set (the third parameter is omitted), then All values starting at the specified position will be deleted. 2. Use "array_slice($arr, position)" to delete all values before the specified position.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php remove the array part Two methods of value
Method 1: Use array_splice() function
array_splice() function can delete the specified number starting from the specified position Elements
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24,23,45,78,-1); var_dump($arr); echo "删除后的数组:" ; array_splice($arr,2,4); var_dump($arr); ?>
Method 2: Use the array_slice() function
array_slice() to intercept all elements starting at the specified position, That is, delete all elements before the specified position.
<?php header("Content-type:text/html;charset=utf-8"); $arr=array(10,12,20,25,24,23,45,78,-1); var_dump($arr); echo "删除后的数组:" ; var_dump(array_slice($arr,2)); var_dump(array_slice($arr,4)); var_dump(array_slice($arr,6)); var_dump(array_slice($arr,8)); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove partial values from an array in php. For more information, please follow other related articles on the PHP Chinese website!