Home >Backend Development >PHP Problem >Can php multidimensional arrays be duplicated?

Can php multidimensional arrays be duplicated?

WBOY
WBOYOriginal
2023-05-19 12:03:37582browse

PHP is a powerful programming language that supports multi-dimensional arrays and array deduplication. During the development process, we often need to use multi-dimensional arrays for data management and processing, and data deduplication is also essential. So the question is, can multi-dimensional arrays in PHP be duplicated? This article will discuss the deduplication method of multi-dimensional arrays in PHP.

1. PHP multidimensional array

In PHP, a multidimensional array is an array containing multiple nested arrays. For example, we can use a multidimensional array to store student performance information:

$scores = array(
    "张三" => array("语文" => 85, "数学" => 90, "英语" => 98),
    "李四" => array("语文" => 91, "数学" => 88, "英语" => 95),
    "王五" => array("语文" => 92, "数学" => 95, "英语" => 90)
);

In the above example, we created a multidimensional array containing three nested arrays, each nested array contains A student's performance information. Note that in multi-dimensional arrays, each array has a corresponding key value (Zhang San, Li Si, Wang Wu), and this key value can be a number or a string type.

2. Deduplication method

  1. array_unique() function

array_unique() function is a built-in function in PHP for deduplication. It can Remove duplicate values ​​from the array and retain the key values ​​of the original array. However, for multi-dimensional arrays, the array_unique() function can only remove duplicate values ​​from the first-level subarray. For example:

$array = array(1,1,'a','b',array('a','b'),array('a','b')); 
$result = array_unique($array);
print_r($result);

The output result is:

Array
(
    [0] => 1
    [2] => a
    [3] => b
    [4] => Array
        (
            [0] => a
            [1] => b
        )

)

We found that although the nested arrays ('a', 'b') were repeated twice, they were not deduplicated.

2. Custom function

For the problem of deduplication of multi-dimensional arrays, we can customize a function to achieve it. The following is a simple example:

function multi_array_unique($array){
    foreach ($array as $key1 => $value1) {
        foreach ($array as $key2 => $value2) {
            if ($key1 != $key2 && $value1 === $value2) {
                unset($array[$key2]);
            }
        }
    }
    return $array;
}

The above function uses a double foreach loop to traverse multi-dimensional arrays. If the two arrays are not the same array and the values ​​are equal, the subsequent array elements are deleted. Finally, the processed array is returned.

We can test this function:

$array = array(
    array("id"=>1,"name"=>"apple"),
    array("id"=>2,"name"=>"banana"),
    array("id"=>3,"name"=>"orange"),
    array("id"=>4,"name"=>"orange"),
    array("id"=>5,"name"=>"apple")
);
$result = multi_array_unique($array);
print_r($result);

The output result is:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => apple
        )

    [1] => Array
        (
            [id] => 2
            [name] => banana
        )

    [2] => Array
        (
            [id] => 3
            [name] => orange
        )

)

We can see from the output result that the duplicate values ​​​​in the multi-dimensional array have been removed. Only unique values ​​are retained.

3. Summary

PHP multi-dimensional arrays can be deduplicated through custom functions to improve the execution efficiency and accuracy of the program. In actual development, it is necessary to choose a suitable deduplication method based on project requirements. The above are some of my experiences and ideas, welcome to exchange and discuss!

The above is the detailed content of Can php multidimensional arrays be duplicated?. 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