Home >Backend Development >PHP Problem >How to delete the i-th element of an array in php
In PHP, you can use the array_splice() function to delete the i-th element of the array. This function can delete a specified number of elements starting from the specified position; the syntax for deleting the i-th element of the array is " array_splice($arr,i-1,1);".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_splice () function to delete the i-th element of the array.
Example:
There is such an array:
<?php $arr=array(10,12,20,25,24); echo "原数组:"; var_dump($arr); ?>
Delete the second element of the array
echo "删除数组第2个元素:" ; array_splice($arr,1,1); var_dump($arr);
Delete the 3rd element of the array
echo "删除数组第3个元素:" ; array_splice($arr,2,1); var_dump($arr);
Delete the 2nd and 3rd elements of the array
array_splice($arr,1,2); var_dump($arr);
Description:
array_splice() function is used to delete part of the elements of the array; you can delete them directly or replace them with other values.
array_splice() syntax is as follows:
array array_splice ( array &$arr, int $start [, int $length = 0 [, mixed $replacement ]] )
Parameter description:
If the combination of start and length results in no element being deleted, then the value contained in replacement will be inserted into the position specified by start.
Note that using replacement to replace array elements will not retain the original key names.
Return value: Returns an array consisting of the deleted elements.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to delete the i-th element of an array in php. For more information, please follow other related articles on the PHP Chinese website!