Home  >  Article  >  Backend Development  >  How to remove elements from a two-dimensional array in php

How to remove elements from a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-26 09:10:48644browse

In PHP, if we need to remove certain elements from a two-dimensional array, we can use some basic array functions to achieve this. In this article, we will introduce two methods to remove elements from two-dimensional arrays in PHP.

Method 1: Use array_filter and array_map functions

array_filter can filter values ​​that do not meet the conditions in the array, and array_map can apply the specified callback function to each element in the array. We can combine the two functions to filter out the elements that meet the conditions in the array and complete the elimination operation.

For example, if we need to remove all elements whose age is less than or equal to 30 years old from the following two-dimensional array:

$students = array(
    array('name' => 'John', 'age' => 25, 'grade' => 'A'),
    array('name' => 'Jane', 'age' => 35, 'grade' => 'B'),
    array('name' => 'Bob', 'age' => 28, 'grade' => 'C'),
    array('name' => 'Alice', 'age' => 30, 'grade' => 'D')
);

We can use the following code:

$students = array_map(function($student){
    return array_filter($student, function($value, $key){
        if($key == 'age' && $value <= 30){
            return false;
        }
        return true;
    }, ARRAY_FILTER_USE_BOTH);
}, $students);

This code block, we first apply a function to each element in the $students array using array_map. This function uses the array_filter function to filter out elements that meet the conditions. The condition is that if the key value of the element is 'age' and the value is less than or equal to 30, it returns false, otherwise it returns true. Then, we reorganize each element we obtained into a new two-dimensional array, which is a new array that eliminates elements whose age is less than or equal to 30 years old.

Method 2: Use foreach loop

The code of method 1 is relatively long and difficult to understand. We can use foreach loop to simplify the code. For each element in the two-dimensional array, we can determine whether the deletion condition is met in the loop, and if not, save the subscript of the element to a new array. Finally, we can use the array_diff_key function to compare the original array with the new array to get a new two-dimensional array from which all elements that meet the conditions have been deleted.

For example, if we need to remove all elements whose age is less than or equal to 30 years old from the following two-dimensional array:

$students = array(
    array(&#39;name&#39; => 'John', 'age' => 25, 'grade' => 'A'),
    array('name' => 'Jane', 'age' => 35, 'grade' => 'B'),
    array('name' => 'Bob', 'age' => 28, 'grade' => 'C'),
    array('name' => 'Alice', 'age' => 30, 'grade' => 'D')
);

We can use the following code:

$keys = array();
foreach($students as $key => $student){
    if($student['age'] <= 30){
        $keys[] = $key;
    }
}
$students = array_diff_key($students, array_flip($keys));

This code block, we use a foreach loop to traverse each element in the $students array. If the age of the element is less than or equal to 30, the key of the element is saved to the $keys array. Then, we use the array_diff_key function to compare the original array and the new array to get a new two-dimensional array from which all elements that meet the conditions have been deleted.

Summary

PHP’s array function is powerful. We can choose different functions to complete different tasks according to actual needs. Method 1 uses the array_filter and array_map functions, and the code is relatively long, but the function is more powerful and flexible; Method 2 uses a foreach loop, the code is simple and easy to understand, and is suitable for processing smaller arrays. Regardless of which method, our goal is to remove elements from the two-dimensional array and achieve the desired effect.

The above is the detailed content of How to remove elements from 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