In PHP, if you need to remove a specific value from an array, there are a few different ways to do it. In this article, we will discuss these methods. Suppose we have the following array:
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
array_filter()
The function can be used to filter out the required values in the array and return a new array. We just need to pass a callback function, and this callback function determines the "required value" criteria. In our case, we want to remove the item with value 5 from the array, so we can pass the callback function like this:
function remove_5($value) { return $value != 5; } $arr = array_filter($arr, "remove_5");
This function takes advantage of PHP's weakly typed comparison operator!=
, which automatically performs type conversion when comparing values. This means we don't need to worry about whether the input value is a string or an integer, etc.
array_filter()
The function returns only elements that meet the specified conditions and builds a new array based on their original subscripts. At this point, we should notice that the subscripts of this new array have been reset, so if we want to keep the original subscripts, we need to use another method.
array_search()
The function can be used to find a specified value in an array and return its key. We can use the unset()
function to delete the element corresponding to this key. Our code is as follows:
$key = array_search(5, $arr); if ($key !== false) { unset($arr[$key]); }
In this example, we first use the array_search()
function to find the element with key 5, and if found, we use unset()
function to delete it.
It should be noted that in the array_search()
function, the first parameter is the value to be found, and the second parameter is the array to be searched. If the value is not found, false
is returned.
We can use the array_diff()
function to compare one array with another array and return the difference between the two arrays. In our case, we can use this function to remove the item with value 5 as shown below:
$remove = array(5); $arr = array_diff($arr, $remove);
This function accepts two parameters, which are the two arrays to be compared. It returns an array containing elements that are present in the first array but not in the second array.
The last method requires using a foreach loop to traverse the array and use the unset()
function to delete the required values. The code is as follows:
foreach ($arr as $key => $value) { if ($value == 5) { unset($arr[$key]); } }
This method allows us to access each element directly, and we can modify the array itself in the loop. However, this method may affect performance due to its high complexity, which may cause performance issues if the array is large.
In PHP, there are many ways to delete specified elements from an array. You can use the array_filter()
function to return elements that do not need to be deleted, or you can use the array_search()
function and the unset()
function to delete elements that need to be deleted. element. We can also use the array_diff()
function to compare two arrays and return the difference. Finally, we can use loops and the unset()
function to iterate through the array and delete specified elements directly. In specific applications, you can choose the method that best suits you based on actual needs.
The above is the detailed content of How to remove specified value from php array. For more information, please follow other related articles on the PHP Chinese website!