Home  >  Article  >  Backend Development  >  php remove specified from array

php remove specified from array

WBOY
WBOYOriginal
2023-05-11 10:33:06382browse

PHP is a widely used server-side scripting language, especially suitable for web development. In PHP, arrays are one of the frequently used data structures. In the actual programming process, it is often necessary to add, modify, and remove arrays. This article will focus on how to remove specified elements from an array in PHP.

  1. Use the unset() function

The unset() function is a function provided by PHP for destroying variables. When we want to remove a specified element from an array, we can pass the key of the element as a parameter to the unset() function.

The sample code is as follows:

$fruits = array("apple", "banana", "orange", "grape");
unset($fruits[1]);  //移除"banana"
var_dump($fruits);

After executing the above code, the output result is:

array(3) { [0]=> string(5) "apple" [2]=> string(6) "orange" [3]=> string(5) "grape" }

You can see that after executing the unset() function, the original $fruits array No longer contains the element "banana".

  1. Use the array_diff() function

The array_diff() function can be used to calculate the difference between two arrays, that is, return an array that is contained in the first array, but A new array that does not contain values ​​in other arrays. Therefore, when we want to remove one or more specified elements from an array, we can get the new array after removing the specified elements by taking the difference between the array composed of the elements and the original array.

The sample code is as follows:

$fruits = array("apple", "banana", "orange", "grape");
$to_remove = array("banana", "orange");
$new_fruits = array_diff($fruits, $to_remove);
var_dump($new_fruits);

After executing the above code, the output result is:

array(2) { [0]=> string(5) "apple" [3]=> string(5) "grape" }

It can be seen that after executing the array_diff() function, the $new_fruits array has The two elements "banana" and "orange" are no longer included.

  1. Use the array_filter() function

The array_filter() function can be used to filter elements in an array, and can filter out elements that do not meet the conditions according to custom rules. When we want to remove elements from the array that meet the conditions, we can use it as the callback function of the array_filter() function and then return a "false" value.

The sample code is as follows:

$fruits = array("apple", "banana", "orange", "grape");
$new_fruits = array_filter($fruits, function($value){
    return $value !== "banana" && $value !== "orange";
});
var_dump($new_fruits);

After executing the above code, the output result is:

array(2) { [0]=> string(5) "apple" [3]=> string(5) "grape" }

It can be seen that after using the array_filter() function, the $new_fruits array has The two elements "banana" and "orange" are no longer included.

To sum up, PHP provides a variety of methods for removing specified elements from an array, and developers can choose the appropriate method according to actual needs. Among them, the unset() function is the simplest way, while the array_diff() function and array_filter() function are more flexible and suitable for more situations.

The above is the detailed content of php remove specified from array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn