Home >Backend Development >PHP Problem >How to delete a certain value from a php array
In the PHP development process, we often need to operate arrays, and one of the common operations is to delete a certain value. This article will introduce several methods to delete a certain value from an array in PHP.
Method 1: unset()
The unset() function in PHP can be used to destroy the value of a specified variable. For arrays, use the unset() function to delete specified elements in the array. The sample code is as follows:
<?php $array = array('a', 'b', 'c', 'd'); // 删除数组中第二个元素 unset($array[1]); print_r($array); ?>
The running result is:
Array ( [0] => a [2] => c [3] => d )
As you can see, use the unset() function to delete After the element at the specified index is removed from the array, the key value of the array is also updated.
Method 2: array_splice()
The array_splice() function in PHP can be used to remove a segment of elements from an array and replace them with another segment of elements. If you only want to delete one element, you can use the following code:
<?php $array = array('a', 'b', 'c', 'd'); // 删除数组中第二个元素 array_splice($array, 1, 1); print_r($array); ?>
The running result is:
Array ( [0] => a [1] => c [2] => d )
array_splice() function needs to pass three parameters: the array to be operated, start deletion or The index of the replacement element, and the number of elements to remove or replace. In this example, we remove the second element of the array (index 1) from the array, removing only one element.
Method 3: array_diff()
The array_diff() function in PHP can be used to compare the differences between two or more arrays and return the difference value. If you only want to delete one element in the array, you can pass the element to be deleted as a new array to the array_diff() function, as shown below:
<?php $array = array('a', 'b', 'c', 'd'); // 删除数组中的'b'元素 $new_array = array_diff($array, array('b')); print_r($new_array); ?>
The running result is:
Array ( [0] => a [1] => c [2] => d )
Yes See, by passing the element to be removed from the array as a function argument to the array_diff() function, you can return a new array that does not contain that element.
Method 4: array_filter()
The array_filter() function in PHP can be used to filter values in an array. We can delete specified elements in the array by adding a deletion rule in a callback function. The sample code is as follows:
<?php $array = array('a', 'b', 'c', 'd'); // 删除数组中的'b'元素 $new_array = array_filter($array, function($value) { return $value != 'b'; }); print_r($new_array); ?>
The running result is:
Array ( [0] => a [2] => c [3] => d )
As you can see, by adding a callback function , in which it is judged whether the element in the array is equal to the element to be deleted, and then it can be removed.
Summary
The above are several common array deletion methods in PHP. Each method has its applicable scenarios, and developers can choose different solutions according to their needs. The above sample codes can be run in PHP 5.3 and above.
The above is the detailed content of How to delete a certain value from a php array. For more information, please follow other related articles on the PHP Chinese website!