How to delete elements by value in php two-dimensional array

PHPz
Release: 2023-04-27 14:46:30
Original
595 people have browsed it

In PHP, arrays are a very common data structure, among which two-dimensional arrays are even more commonly used. However, when using a two-dimensional array, sometimes we need to delete an element in it. At this time, we need to consider deleting the element by value. This article will introduce how to delete elements from a two-dimensional array by value in PHP.

1. Use foreach loop to delete elements

The simplest way to delete elements by value in a two-dimensional array is to use a foreach loop to traverse the array, and then determine whether each element in the array is equal to The value we want to delete, if equal, delete the element. The code is as follows:

$arr = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
);

foreach ($arr as $k => $v) {
    if ($v['name'] == '李四') {
        unset($arr[$k]);
    }
}
Copy after login

In the above code, we first define a two-dimensional array $arr, and then use a foreach loop to traverse the array to determine the "name" of each element in the array "Whether the key is equal to "John Doe", if so, use the unset() function to delete the element from the array. The final array will not contain the element "John Doe".

It should be noted that when using the unset() function to delete elements, you must use $k as the key name instead of $v, otherwise an error will occur.

2. Use the array_filter function to delete elements

Another way to delete elements in a two-dimensional array by value is to use the array_filter function, which can use a callback function for each element in the array Filters the elements and returns a new array that does not contain the filtered elements. The code is as follows:

$arr = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
);

$arr = array_filter($arr, function($v) {
    return $v['name'] != '李四';
});
Copy after login

In the above code, we pass the callback function as the second parameter to the array_filter function. The callback function accepts each element in the array as a parameter and returns a Boolean value. If it returns true, it means that the element will be retained in the new array, if it returns false, it means that the element will be removed from the new array.

In the above code, we use an anonymous function as a callback function to determine whether the "name" key of each element is equal to "李思". If not, return true, indicating that the element will be retained in in the new array, otherwise false is returned, indicating that the element will be deleted from the new array.

It should be noted that the array_filter function will return a new array, so we need to save the returned array to the original array to obtain the final array with the specified elements deleted.

3. Use the array_search function to delete elements

Another way to delete elements in a two-dimensional array by value is to use the array_search function, which can be used to find the key of a specified value in an array , and returns its key name. The code is as follows:

$arr = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
);

$key = array_search('李四', array_column($arr, 'name'));

if ($key !== false) {
    unset($arr[$key]);
}
Copy after login

In the above code, we use the array_search function to find the key of '李思' in the array. This function accepts two parameters. The first parameter is the value to be found, and the second The first parameter is the array to search for. In the above code, we extract the two-dimensional array into a one-dimensional array by column, and then search for the key name of '李思' in the one-dimensional array. Once the key name is found, we use the unset() function to unset the element corresponding to the key. delete.

It should be noted that the array_search function returns the key name of the search result. If the search fails, it returns false. We need to use the !== operator to check whether the return value is false. If not, it means that the search is successful and the specified element can be deleted. Otherwise, the specified element cannot be found.

Summary

There are many ways to delete two-dimensional array elements by value. The above introduces three common methods:

Use a foreach loop to traverse the array and determine each Whether the value of the element is equal to the value to be deleted, if so, use the unset() function to delete the element.

Use the array_filter function to filter the array, and use the callback function to determine whether each element needs to be retained in the new array.

Use the array_search function to find the key with the specified value in the array, and then use the unset() function to delete the element corresponding to the key.

In actual development, we can choose different methods to delete array elements according to specific needs to achieve optimal performance and efficiency.

The above is the detailed content of How to delete elements by value in php two-dimensional array. 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