Home  >  Article  >  Backend Development  >  How to remove specified value from array in php

How to remove specified value from array in php

PHPz
PHPzOriginal
2023-04-27 16:24:15551browse

In PHP, we usually use arrays to store large amounts of data. Sometimes, we need to remove certain elements (that is, specified values) in the array for subsequent data processing. This article will introduce how to remove the specified value in the array through PHP code.

Method 1: Use the array_filter() function

The array_filter() function is a very useful function in PHP and can be used to filter elements in an array. This function iterates through each element in the array and passes it to the callback function for processing. If the callback function returns true, the element will be retained, otherwise it will be filtered out. Therefore, we can use this function to filter out the elements that need to be retained.

The following is a sample code that uses the array_filter() function to remove specified values ​​​​in an array:

// 定义一个数组
$fruits = array("apple", "banana", "orange", "pear", "kiwi");

// 定义需要移除的元素
$remove = "orange";

// 使用array_filter()函数过滤掉需要移除的元素
$fruits = array_filter($fruits, function ($value) use ($remove) {
    return $value != $remove;
});

// 输出过滤后的数组
print_r($fruits);

Output results:

Array
(
    [0] => apple
    [1] => banana
    [3] => pear
    [4] => kiwi
)

In this example, we first define an array $fruits, adds five elements (fruit names) to the array. Then we define $remove the element that needs to be removed, in this case "orange". Next, we use the array_filter() function and pass the callback function in for filtering. The logic in the callback function is: if the value of the element is not equal to $remove, return true, indicating that the element is retained; otherwise, return false, indicating that the element is filtered out. Finally, the filtered array is output through the print_r() function.

Method 2: Use foreach loop

If you don’t want to use the array_filter() function, you can also use foreach loop to traverse the array and remove the specified value. The specific implementation logic is: traverse each element of the array, and if the specified value is found, remove the element from the array.

The following is a code example that uses a foreach loop to remove a specified value:

// 定义一个数组
$fruits = array("apple", "banana", "orange", "pear", "kiwi");

// 定义需要移除的元素
$remove = "orange";

// 使用foreach循环移除指定值的元素
foreach ($fruits as $key => $value) {
    if ($value == $remove) {
        unset($fruits[$key]);
    }
}

// 输出移除指定值后的数组
print_r($fruits);

Output result:

Array
(
    [0] => apple
    [1] => banana
    [3] => pear
    [4] => kiwi
)

In this example, a foreach loop is used to traverse the items in the array $fruits For each element, if the specified value $remove is found, use the unset() function to remove the element from the array. Finally, the array after removing the specified value is output through the print_r() function.

Conclusion

Whether you use the array_filter() function or the foreach loop, you can effectively remove the specified value in the array. Which method to use depends on your preference and application scenario. If the array is very large, consider using the array_filter() function as it simplifies the code and is more efficient. If the array is small, you can use a foreach loop, which is also very simple and easy to understand.

Of course, we can also use other data structures, such as linked lists, queues, etc., to store data, but this is beyond the scope of this article. Hope this article is helpful to you.

The above is the detailed content of How to remove specified value from array in php. 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