In PHP development, arrays are a very common data structure. When using arrays, sometimes we need to delete an element in the array. PHP provides some methods to delete specified elements from an array. This article will introduce you to PHP's method of deleting specified elements from an array.
1. Use the unset function to delete elements
PHP’s unset function can be used to delete a variable or an element in an array. The specific usage is as follows:
unset($array[index]);
Among them, $array is the array of elements to be deleted, and index is the index specifying the element to be deleted.
For example, the code is as follows:
$arr = array('one', 'two', 'three', 'four', 'five'); unset($arr[2]); print_r($arr);
The running result is:
Array ( [0] => one [1] => two [3] => four [4] => five )
It can be seen that after deleting elements in the array through the unset function, the index position of the deleted element is Once empty, the index of the original array will be rebuilt.
2. Use the array_splice function to delete elements
PHP’s array_splice function can insert or delete specified elements or a range of elements in an array. Its usage is:
array_splice($input, $offset, $length, $replacement);
where $input is the array to be operated on, $offset is the index of the starting position, $length is the number of subsequent elements to delete, and $replacement is the optional replacement element. array.
For example, the code is as follows:
//删除一个元素 $arr = array('one', 'two', 'three', 'four', 'five'); array_splice($arr, 2, 1); print_r($arr); //删除多个元素 $arr = array('one', 'two', 'three', 'four', 'five'); array_splice($arr, 1, 2); print_r($arr);
The running result is:
Array ( [0] => one [1] => two [2] => four [3] => five ) Array ( [0] => one [1] => four [2] => five )
It can be seen that after using the array_splice function, the index of the original array will not be reconstructed, but Leave it as is.
3. Use the array_diff function to delete elements
PHP’s array_diff function can compare the values of two or more arrays and return different values. We can use this function to delete a specified element. The specific usage is as follows:
$array = array_diff($array, array($value));
Among them, $array is the array of elements to be deleted, and $value is the specified value to be deleted.
For example, the code is as follows:
$arr = array('one', 'two', 'three', 'four', 'five'); $arr = array_diff($arr, array('three')); print_r($arr);
The running result is:
Array ( [0] => one [1] => two [3] => four [4] => five )
It can be seen that after using the array_diff function, the index of the original array will also be rebuilt.
The above are the three methods of PHP to delete the elements specified in the array. Different methods are suitable for different scenarios. You can choose the appropriate method to delete specified elements in the array according to the actual situation.
The above is the detailed content of How to delete specified elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!