Home > Backend Development > PHP Problem > PHP two-dimensional array deletes the specified one-dimensional array

PHP two-dimensional array deletes the specified one-dimensional array

王林
Release: 2023-05-06 13:27:07
Original
684 people have browsed it

In PHP programming, two-dimensional arrays are a very common data type. Two-dimensional arrays are also used to store and process complex data. In some cases, we need to remove a specific one-dimensional array from a two-dimensional array. This process often appears more difficult in PHP. In this article, we will learn how to delete a specified one-dimensional array from a two-dimensional array.

Let’s first look at a simple two-dimensional array example, which contains three one-dimensional arrays:

$students = array(
    array("name" => "John", "age" => 20, "skill" => "PHP"),
    array("name" => "Mary", "age" => 22, "skill" => "Java"),
    array("name" => "Peter", "age" => 21, "skill" => "Python")
);
Copy after login

Suppose we want to delete the one-dimensional array named Mary, how should we achieve it? Woolen cloth?

Method 1: Use foreach loop to achieve

One of the feasible solutions is to use foreach loop to obtain each one-dimensional array, and use the unset() function in each iteration to delete the conditions one-dimensional array.

foreach($students as $key => $value) {
  if($value['name'] == 'Mary') {
    unset($students[$key]);
  }
}
Copy after login

In this example, $key is the key of the current iteration, and $value is the value of the current iteration. In each iteration, we compare the name key to determine whether the condition is met. If a matching array is found, we delete the array using the unset() function.

Method 2: Use array_filter() to implement

Another way to delete a specified one-dimensional array is to use the array_filter() function. This function accepts an array as input and returns a new array with elements that meet the criteria removed.

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

In this example, we pass the anonymous function to the array_filter() function. This function will iterate over the $students array and pass each one-dimensional array as an input array to the function. In the anonymous function, we use the !== operator to compare whether the name key of the current array is equal to Mary. If they are not equal, the array_filter() function returns a new array and the elements that meet the conditions are retained.

Method 3: Use array_search() to implement

Finally, we can use the array_search() function to find the one-dimensional array to be deleted, and then use the unset() function to delete it.

$search_key = array_search('Mary', array_column($students, 'name'));
unset($students[$search_key]);
Copy after login

Note that we use the array_column() function to return the values ​​of all name keys from the $students array as a new array. We can use the array_search() function to search for a matching value in this new array and return the key of that value. Finally, we use the unset() function to delete the element corresponding to the key.

Summary:

The above are three common methods for deleting a specified one-dimensional array from a PHP two-dimensional array. It's important to note that each method has its pros and cons. When choosing an implementation method, we should choose the most appropriate way based on our code needs.

In actual applications, the implementation method is selected based on the actual situation. Sometimes nested loops can also solve the problem, but if the amount of data is too large, it may be inefficient, so a good balance between performance and Code readability.

The above is the detailed content of PHP two-dimensional array deletes the specified one-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