In PHP, when we need to process arrays, loops are often used. Looping is a very convenient way to process arrays, but sometimes we need to remove certain elements from the array. What should we do at this time? Today we will introduce several common ways to remove array elements in a loop.
1. Use the unset() function
The unset() function is a very commonly used function in PHP. We can use it to delete array elements in a loop. The specific implementation is as follows:
$fruits = array("apple", "banana", "orange", "kiwi"); foreach ($fruits as $key => $value) { if ($value == "banana") { unset($fruits[$key]); } } print_r($fruits); //输出结果:Array ( [0] => apple [2] => orange [3] => kiwi )
In the above code, we first define a fruit array $fruits, and then use the foreach() loop to traverse each element in the array. If the element value is equal to "banana", then Use the unset() function to remove the element from the array.
It should be noted that when using the unset() function, you need to use the key name of the array (that is, $key in the above code).
2. Use array_splice() function
array_splice() function is a common function for inserting or deleting elements in an array. We can use it to remove arrays in a loop. element. The specific implementation is as follows:
$fruits = array("apple", "banana", "orange", "kiwi"); foreach ($fruits as $key => $value) { if ($value == "banana") { array_splice($fruits, $key, 1); } } print_r($fruits); //输出结果:Array ( [0] => apple [1] => orange [2] => kiwi )
In the above code, we first define a fruit array $fruits, and then use the foreach() loop to traverse each element in the array. If the element value is equal to "banana", then Use the array_splice() function to remove the element from the array.
It should be noted that the array index here starts from 0.
3. Use the array_filter() function
The array_filter() function is a very commonly used function in PHP. We can use it to remove array elements in a loop. The specific implementation is as follows:
$fruits = array("apple", "banana", "orange", "kiwi"); $fruits = array_filter($fruits, function($value) { return $value != "banana"; }); print_r($fruits); //输出结果:Array ( [0] => apple [2] => orange [3] => kiwi )
In the above code, we first define a fruit array $fruits, and then use the array_filter() function to filter the elements in the array whose value is not "banana" using the callback function. come out.
It should be noted that when using the array_filter() function, the return value of the callback function is true and false. If the return value is true, the element is retained; if the return value is false, the element is removed. .
Summary
Through the above introduction, we can find that there are many ways to loop through an array to remove elements in PHP, but no matter which method is used, you need to be careful handle to avoid unexpected situations. At the same time, when using the unset() function, you need to use the key name of the array when determining the array element to be deleted; when using the array_splice() function, you need to note that the array index starts from 0; when using the array_filter() function, You need to use the callback function to filter array elements.
The above is the detailed content of How to loop through an array in php and remove elements. For more information, please follow other related articles on the PHP Chinese website!