Home > Backend Development > PHP Problem > How to remove subarrays from an array in php

How to remove subarrays from an array in php

PHPz
Release: 2023-04-20 15:18:12
Original
609 people have browsed it

In PHP, sometimes we need to remove another array from an array, which involves array operations. In this article, we will introduce some methods to remove arrays from an array.

1. Use the foreach loop

First, we can use the foreach loop to traverse the array, and then use the in_array function to determine whether the array element appears in the target array, and if it does, use unset The function removes it from the target array.

Sample code:

$arr1 = array('a', 'b', 'c', 'd');
$arr2 = array('a', 'e', 'f');
foreach ($arr2 as $value) {
    if (in_array($value, $arr1)) {
        unset($arr1[array_search($value, $arr1)]);
    }
}
print_r($arr1);
Copy after login

The above code will output ['b', 'c', 'd'], that is, the elements present in $arr2 are removed from $arr1.

2. Use the array_diff function

Another method that can be used is through the array_diff function, which can return a new array composed of different values ​​​​in the two arrays. Therefore, we can pass the target array and the array to be removed as two parameters to the function, and we can get the new array after removal.

Sample code:

$arr1 = array('a', 'b', 'c', 'd');
$arr2 = array('a', 'e', 'f');
$arr3 = array_diff($arr1, $arr2);
print_r($arr3);
Copy after login

The above code will also output ['b', 'c', 'd'], that is, the elements present in $arr2 are removed from $arr1.

3. Use the array_filter function

The last method is to use the array_filter function, which can use a callback function to filter array elements. We can use the callback function to filter the elements in the target array that are the same as the array elements to be removed, and finally get the new array after removal.

Sample code:

$arr1 = array('a', 'b', 'c', 'd');
$arr2 = array('a', 'e', 'f');
$arr3 = array_filter($arr1, function($item) use($arr2) {
    return !in_array($item, $arr2);
});
print_r($arr3);
Copy after login

The above code will also output ['b', 'c', 'd'], that is, the elements present in $arr2 are removed from $arr1.

Summary

This article introduces three methods to remove arrays from an array, using the foreach loop, array_diff function and array_filter function. In actual development, we can choose the appropriate method according to the specific situation.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template