Home >Backend Development >PHP Problem >How to delete specified elements in an array in php
php method to delete specified elements in an array: You can use the array_splice() function to delete, such as [array_splice($array, 1, 1);]. The array_splice() function removes selected elements from an array and replaces them with new elements.
Function introduction:
(Video tutorial sharing: php video tutorial)
array_splice( ) function removes a selected element from an array and replaces it with a new element. The function will also return an array of removed elements.
Tip: If the function does not remove any elements (length=0), the replacement array will be inserted from the position of the start parameter.
Note: Key names in the replacement array are not retained.
Grammar:
array_splice(array,start,length,array)
Code implementation:
<?php $array = array(0 => "a", 1 => "b", 2 => "c"); array_splice($array, 1, 1); //↑ Offset which you want to delete print_r($array); ?>
Output result:
Array( [0] => a [1] => c )
Related recommendations: php training
The above is the detailed content of How to delete specified elements in an array in php. For more information, please follow other related articles on the PHP Chinese website!