Home > Backend Development > PHP Problem > How to delete specified elements in a two-dimensional array in php

How to delete specified elements in a two-dimensional array in php

PHPz
Release: 2023-04-18 14:36:30
Original
1094 people have browsed it

In PHP, we have many methods to delete array elements. But when we want to delete an element in a two-dimensional array, more operations and thinking are required. In this article, we will introduce some methods to delete specified elements in a two-dimensional array.

1. Use foreach loop to delete specified elements

If we want to delete specified elements in a two-dimensional array, we can use foreach loop to traverse the entire array and then find the element that needs to be deleted. Once we find the element that needs to be removed, we can use the unset() function to remove the element.

This is a sample code:

<?php
//定义一个二维数组
$employees = array(
   array("name"=>"John", "age"=>20),
   array("name"=>"Peter", "age"=>25),
   array("name"=>"Kevin", "age"=>30)
);

//遍历数组
foreach ($employees as $key => $value) {

    //如果找到需要删除的元素
    if ($employees[$key]["name"] == "Peter") {

        //删除元素
        unset($employees[$key]);

    }

}

//重新索引数组
$employees = array_values($employees);

//输出结果
print_r($employees);
?>
Copy after login

In the above code, we define a two-dimensional array named $employees. We iterate through this array and find the element named "Peter". Once we find the element, we use the unset() function to delete it. Finally, we use the array_values() function to re-index the array and print the results.

2. Use the array_filter() function to delete the specified element

You can use the array_filter() function in PHP. This function will accept an array as a parameter and return a new array containing Filtered elements. We can define the items we want to filter in the callback function and return the new array.

This is a sample code:

<?php
//定义一个二维数组
$employees = array(
   array("name"=>"John", "age"=>20),
   array("name"=>"Peter", "age"=>25),
   array("name"=>"Kevin", "age"=>30)
);

//使用array_filter()函数过滤数组
$employees = array_filter($employees, function($employee){
    return $employee["name"] != "Peter";
});

//输出结果
print_r($employees);
?>
Copy after login

In the above code, we define a two-dimensional array named $employees. We use the array_filter() function to filter out elements named "Peter". We define the filtering conditions in the callback function and return the new array. Finally, we use the print_r() function to print the output results.

3. Use array_search() function and unset() function to delete specified elements

We can also use array_search() function and unset() function to delete specified elements. The array_search() function can search for a specified value in an array and return its key. Once we find the element that needs to be removed, we can remove it using the unset() function.

This is a sample code:

<?php
//定义一个二维数组
$employees = array(
   array("name"=>"John", "age"=>20),
   array("name"=>"Peter", "age"=>25),
   array("name"=>"Kevin", "age"=>30)
);

//寻找数组中的需要删除的元素
$key = array_search("Peter", array_column($employees, "name"));

//删除元素
unset($employees[$key]);

//重新索引数组
$employees = array_values($employees);

//输出结果
print_r($employees);
?>
Copy after login

In the above code, we define a two-dimensional array named $employees. We use the array_column() function to retrieve all the names in the array and the array_search() function to find the element that needs to be removed. Once we find the element, we use the unset() function to delete it. Finally, we use the array_values() function to re-index the array and print the results.

The above are several methods to delete specified elements of a two-dimensional array. In actual development, you can choose a suitable method according to specific business needs. Hope this article is helpful to you.

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