Home > Article > Backend Development > How to delete a value in an array in php
How to delete a value in an array in php: You can use the php built-in function array_splice() to delete. This function can remove the selected element from the array, replace it with the new element, and return the array of the removed element, such as [array_splice($array, 1, 1)].
array_splice() function removes the selected element from an array and replaces it with a new element. The function will also return an array of removed elements.
(Recommended tutorial: php graphic tutorial)
Syntax:
array_splice(array,start,length,array)
Parameters:
array Required. Specifies array
start Required. numerical value. Specifies the starting position of deleted elements
length Optional. numerical value. Specifies the number of elements to be removed, which is also the length of the returned array
array Optional. Specifies an array with elements to be inserted into the original array
(Video tutorial recommendation: php video tutorial)
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 )
The above is the detailed content of How to delete a value in an array in php. For more information, please follow other related articles on the PHP Chinese website!