Home  >  Article  >  Backend Development  >  How to delete a certain subarray in a two-dimensional array in php

How to delete a certain subarray in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-20 13:51:46642browse

In the PHP development process, array is an extremely important data type, which can easily store and represent large amounts of data. Sometimes, we need to remove certain arrays from a two-dimensional array. What should we do? This article will introduce two different methods to achieve this purpose.

Method 1: Use the array_filter function

The array_filter function is a built-in function in PHP, used to filter elements in an array. We can use this function to remove certain arrays from a two-dimensional array. The specific implementation is as follows:

function removeArray($arr, $remove)
{
    return array_filter($arr, function ($item) use ($remove) {
        return !in_array($item, $remove);
    });
}

In the above code, we define a function named removeArray, which accepts two parameters:

  • $arr: indicates that the array needs to be removed from it two-dimensional array.
  • $remove: Indicates the array set that needs to be removed.

Next, we use the array_filter function to filter the $arr array. In the callback function, we use the in_array function to determine whether the currently traversed array needs to be eliminated. Returns false if it needs to be eliminated, true otherwise. Finally, the filtered array is the result we need.

Use case:

$data = [
    ['name' => 'Tom', 'age' => 20],
    ['name' => 'Jerry', 'age' => 18],
    ['name' => 'Andy', 'age' => 22],
    ['name' => 'John', 'age' => 25]
];

$remove = [
    ['name' => 'Tom', 'age' => 20],
    ['name' => 'John', 'age' => 25]
];

$result = removeArray($data, $remove);

print_r($result);

Output result:

Array
(
    [1] => Array
        (
            [name] => Jerry
            [age] => 18
        )

    [2] => Array
        (
            [name] => Andy
            [age] => 22
        )

)

Method 2: Use the array_udiff function

Another method is to use the array_udiff function. This function is used to calculate the difference set of arrays. It can customize a callback function to compare arrays to achieve the required functions.

The specific implementation is as follows:

function removeArray($arr, $remove)
{
    return array_udiff($arr, $remove, function ($a, $b) {
        return ($a == $b) ? 0 : 1;
    });
}

In the above code, we are similar to method 1 and define a function named removeArray. The function accepts two parameters, $arr and $remove, which respectively represent the array set from which the array needs to be removed and the array set that needs to be removed.

We use the array_udiff function to compare the $arr array and the $remove array, execute the custom function, compare the elements in the array and return the result.

Use case:

$data = [
    ['name' => 'Tom', 'age' => 20],
    ['name' => 'Jerry', 'age' => 18],
    ['name' => 'Andy', 'age' => 22],
    ['name' => 'John', 'age' => 25]
];

$remove = [
    ['name' => 'Tom', 'age' => 20],
    ['name' => 'John', 'age' => 25]
];

$result = removeArray($data, $remove);

print_r($result);

Output result:

Array
(
    [1] => Array
        (
            [name] => Jerry
            [age] => 18
        )

    [2] => Array
        (
            [name] => Andy
            [age] => 22
        )

)

To sum up, there are two ways to eliminate certain arrays from a two-dimensional array, namely Use array_filter function and array_udiff function. Both are commonly used methods, and which one to use depends on your needs.

It should be noted that if the same subarray exists in the array, method one (array_filter) will not work well, because arrays cannot be directly compared in PHP, and the array_diff_assoc function needs to be used.

The above is the detailed content of How to delete a certain subarray in a two-dimensional 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