Home  >  Article  >  Backend Development  >  How to delete the same elements in two-dimensional array in php

How to delete the same elements in two-dimensional array in php

PHPz
PHPzOriginal
2023-04-26 09:09:26938browse

In PHP programming, we often encounter situations where we need to operate two-digit arrays. In some cases, we need to remove identical elements to keep the data unique. This article will introduce how to delete identical elements in a two-digit array.

1. What is a two-digit array

A two-digit array (also known as a "multidimensional array" or "array of arrays") is a data structure that contains other arrays. In PHP, we usually use arrays to store and manipulate data. A two-digit array is a special type of array in which each element is an array. We can access the elements in the two-digit array through two or more subscripts, for example:

$students = array(
  array("name"=>"张三", "age"=>18, "score"=>89),
  array("name"=>"李四", "age"=>22, "score"=>76),
  array("name"=>"王五", "age"=>20, "score"=>94)
);

The above code creates a two-digit array containing three elements, each element is a containing An array of three key-value pairs. We can use subscripts to access elements in a two-digit array, for example:

echo $students[0]["name"]; // 输出 张三
echo $students[1]["score"]; // 输出 76
echo $students[2]["age"]; // 输出 20

2. How to delete the same elements in a two-digit array

Sometimes, we will encounter a two-digit array contain the same elements. For example, we may obtain the same student information from different data sources, resulting in duplication of student data. In this case, we need to remove duplicate elements to keep the data unique.

PHP provides a function array_unique() for removing duplicate elements in an array. However, using this function on a two-digit array does not serve our purpose. array_unique() The function can only delete duplicate elements in a one-dimensional array, but cannot delete duplicate elements in a multi-dimensional array.

Therefore, we need to write our own code to remove duplicate elements in the two-digit array. We can use a loop to iterate through each element in the two-digit array, and at each step compare the current element with the previous element to see if it is the same. If they are the same, delete the current element. The following is the code implementation:

function removeDuplicateElements($array){
    $result = array();
    foreach ($array as $key => $value) {
        foreach ($result as $key2 => $value2) {
            if ($value == $value2) {
                unset($array[$key]);
            }
        }
        $result[] = $value;
    }
    return $result;
}

In the above code, we created a function named removeDuplicateElements(), which accepts a two-digit array as a parameter and returns a Remove duplicate elements from a two-digit array.

The function uses two foreach loops internally to traverse the array. The first loop is used to traverse each element in the two-digit array. At each step, a second loop is used to iterate over the elements that have been added to the result array $result and compare the current element to the previous element.

If the current element is the same as the previous element, use the unset() function to delete the current element. unset() The function is used to destroy the specified variable. Here, it is used to remove specified elements from an array. If the current element is not the same as the previous element, the current element is added to the resulting array.

3. Testing and Application

We can create a test function to test the effectiveness of the removeDuplicateElements() function. Here is the code for the test function:

function testRemoveDuplicateElements(){
    $students = array(
      array("name"=>"张三", "age"=>18, "score"=>89),
      array("name"=>"李四", "age"=>22, "score"=>76),
      array("name"=>"王五", "age"=>20, "score"=>94),
      array("name"=>"张三", "age"=>18, "score"=>89),
      array("name"=>"李四", "age"=>22, "score"=>76)
    );

    $result = removeDuplicateElements($students);
    print_r($result);
}

In the above code, we have created a two-digit array with five elements, two of which are repeated. Then, we call the removeDuplicateElements() function to remove duplicate elements and print the result.

When we run the test function, a two-digit array containing three elements will be output, with duplicate elements removed.

This article describes how to delete identical elements in a two-digit array. We use a loop to iterate through the array and compare each element to see if it is the same as the previous element. If they are the same, delete the current element. In this way, we can easily remove duplicate elements in the two-digit array to keep the data unique.

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