Home > Article > Backend Development > How to remove multiple elements from php array
In PHP, array_splice() can be used to remove multiple array elements. This function can delete a specified number of elements starting from a specified position. The syntax is "array_splice(array, starting position, number of deleted)". If the second parameter is a positive array, it will be deleted from front to back; if it is a negative number, it will be deleted from the reciprocal position.
The operating environment of this tutorial: Windows 7 system, PHP 8.1 version, Dell G3 computer.
In php, you can use the array_splice() function to remove multiple array elements.
array_splice() function can delete the specified number of elements starting from the specified position. This function changes the original array. Grammar:
array_splice(array,start,length)
If start
is a positive number, you need to specify the number of deleted elements length
; if start is a negative number, count down from Delete the specified number of elements starting from the position.
Example 1: Remove 2 elements from the beginning
<?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); var_dump($arr); ?>
Example 2: Remove 2 from the middle elements
<?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,2,2); var_dump($arr); ?>
Example 3: Remove 2 elements from the end
<?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,-2); var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove multiple elements from php array. For more information, please follow other related articles on the PHP Chinese website!