Home  >  Article  >  Backend Development  >  How to delete duplicate values ​​from a two-dimensional array in PHP

How to delete duplicate values ​​from a two-dimensional array in PHP

PHPz
PHPzOriginal
2023-04-19 09:20:40628browse

In PHP, we often need to deal with arrays. One of the common problems is how to remove duplicate values ​​in a two-dimensional array. This problem is not difficult to solve. We can use some array functions provided by PHP, such as array_map, array_unique, array_filter, etc. Next, let us take a look at how to delete duplicate values ​​from a two-dimensional array.

What is a two-dimensional array?

Two-dimensional array means that the elements in an array are also an array, and it can be expressed as a matrix. In PHP, we can create a two-dimensional array using the following method:

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

In the above code, we create a $students array, where each element is also an array, containing name and age are two key-value pairs. This array can be understood as a table of student information.

Methods to delete duplicate values

Next, we introduce several methods to delete duplicate values ​​​​from two-dimensional arrays:

Method 1: Use foreach loop

function remove_duplicate_by_foreach($arr){
    $result = array();//定义一个新数组,用来存放去重后的数据
    foreach($arr as $key => $value){
        $flag = false;
        foreach($result as $val){
            if($value == $val){
                $flag = true;
                break;
            }
        }
        if(!$flag){ //如果数据不重复,则将其添加到新数组中
            array_push($result, $value);
        }
    }
    return $result;
}

$students = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
    array('name' => '赵六', 'age' => 18),
    array('name' => '张三', 'age' => 18)
);
print_r(remove_duplicate_by_foreach($students));

In the above code, we first define an empty $result array, and then use two foreach loops to traverse the original array $arr, for each element in it$value, looks for the same element in the $result array, if not, adds it to the $result array.

This method is relatively simple and easy to understand, but the efficiency is relatively low, especially when there is a lot of data, the traversal time will become very long.

Method 2: Use array_map and array_unique functions

function remove_duplicate_by_array_map($arr){
    $temp = array_map("serialize", $arr);//将数组中的每个元素都序列化
    $result = array_unique($temp);//去重操作
    $new_array = array();
    foreach ($result as $value) {//反序列化操作
        array_push($new_array, unserialize($value));
    }
    return $new_array;
}

$students = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
    array('name' => '赵六', 'age' => 18),
    array('name' => '张三', 'age' => 18)
);
print_r(remove_duplicate_by_array_map($students));

In the above code, we first use the array_map function to convert the original array $arr Each element is serialized into a string, then the array_unique function is used to deduplicate the serialized string, and finally the foreach loop is used to deserialize it into a normal array element.

This method is slightly more efficient than method 1, but the serialization and deserialization process may affect performance.

Method 3: Use the array_filter function

function remove_duplicate_by_array_filter($arr){
    $unique_arr = array_unique(array_map('serialize',$arr));//先将数组序列化再去重
    return array_filter($unique_arr, function($var){//回调函数进行反序列化
        return unserialize($var);
    });
}

$students = array(
    array('name' => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22),
    array('name' => '赵六', 'age' => 18),
    array('name' => '张三', 'age' => 18)
);
print_r(remove_duplicate_by_array_filter($students));

In the above code, we first use the array_map function to filter each element in the original array $arr The elements are all serialized into strings, and then the array_unique function is used to deduplicate the serialized string, and finally the array_filter function is used to implement the deserialization operation.

Compared with the first two methods, this method has less code and is more efficient.

Summary

In PHP, it is not difficult to delete duplicate values ​​​​in a two-dimensional array. You can use array_map, array_unique, Array_filter and other functions are implemented. Choose different methods as needed to achieve more efficient processing results.

The above is the detailed content of How to delete duplicate values ​​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