Home >Backend Development >PHP Problem >How to delete specified elements in php array
In PHP, array is a very useful data type and is very common during the development process. Sometimes there will be some elements in our array that we don't need, and we need to delete them from the array. In this article, we will discuss how to delete specified elements from an array in PHP.
1. Use the unset function to delete the specified element
In PHP, we can use the unset function to delete the specified element in the array. The use of the unset function is very simple. You only need to pass the key value corresponding to the element to be deleted. The following is a sample code:
$fruits = array('apple', 'banana', 'grape', 'orange'); unset($fruits[2]); print_r($fruits);
The above code first creates an array named $fruits, then uses the unset function to delete the element (grape) with index 2 in the array, and finally uses the print_r function to output Modified array. The output result is as follows:
Array ( [0] => apple [1] => banana [3] => orange )
You can see that the specified element Grape has been successfully deleted from the array.
2. Use the array_splice function to delete the specified element
In addition to using the unset function, we can also use the array_splice function to delete the specified element in the array. This function can delete any number of elements at a specified position and insert new elements at that position. The following is a sample code:
$fruits = array('apple', 'banana', 'grape', 'orange'); array_splice($fruits, 2, 1); print_r($fruits);
The above code first creates an array named $fruits, then uses the array_splice function to delete 1 element (grape) starting from index 2 in the array, and finally uses The print_r function outputs the modified array. The output result is as follows:
Array ( [0] => apple [1] => banana [2] => orange )
You can see that the specified element Grape has been successfully deleted from the array.
3. Use the array_diff function to delete the specified element
In addition to the above two methods, we can also use the array_diff function to delete the specified element in the array. The array_diff function is used to calculate the difference of multiple arrays. If the same value exists in the specified array, it will be deleted. The following is a sample code:
$fruits = array('apple', 'banana', 'grape', 'orange'); $deleteFruits = array('grape'); $result = array_diff($fruits, $deleteFruits); print_r($result);
The above code first creates an array named $fruits and an array of elements to be deleted named $deleteFruits, and then uses the array_diff function to calculate the $fruits array and remove it. array of the same values in the $deleteFruits array, and use the print_r function to output the modified array. The output result is as follows:
Array ( [0] => apple [1] => banana [3] => orange )
You can see that the specified element Grape has been successfully deleted from the array.
Summary:
This article introduces three methods of deleting specified elements in an array in PHP, including using the unset function, array_splice function and array_diff function. These methods are very simple and easy to use, and the method you choose depends on your actual needs.
The above is the detailed content of How to delete specified elements in php array. For more information, please follow other related articles on the PHP Chinese website!