Home >Backend Development >PHP Problem >How to remove duplicate values ​​in php

How to remove duplicate values ​​in php

藏色散人
藏色散人Original
2022-10-21 09:13:382074browse

php method to remove duplicate values: 1. Use "function more_array_unique($arr=array()){...}" to remove multiple value arrays; 2. Use "$arr_new = more_array_unique($ arr);" method to call the deduplication function; 3. Output the deduplication array.

How to remove duplicate values ​​in php

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to remove duplicate values ​​in php?

php removes duplicate values ​​from the array and returns the result!

array_unique(array) can only process arrays with only a single value.

To remove multiple value arrays, you can use the following function:

function more_array_unique($arr=array()){  
    foreach($arr[0] as $k => $v){  
        $arr_inner_key[]= $k;   //先把二维数组中的内层数组的键值记录在在一维数组中  
    }  
    foreach ($arr as $k => $v){  
        $v =join(",",$v);    //降维 用implode()也行  
        $temp[$k] =$v;      //保留原来的键值 $temp[]即为不保留原来键值  
    }  
    $temp =array_unique($temp);    //去重:去掉重复的字符串  
    foreach ($temp as $k => $v){  
        $a = explode(",",$v);   //拆分后的重组 如:Array( [0] => james [1] => 30 )  
        $arr_after[$k]= array_combine($arr_inner_key,$a);  //将原来的键与值重新合并  
    }  
    return $arr_after;  
}

There are the following arrays:

 $arr = array(  
          '0'=>array(  
                   'name'=>'james',  
                   'age'=>30,  
                   ),  
          '1'=>array(  
                   'name'=>'susu',  
                   'age'=>26,  
                   ),  
           '2'=>array(  
                   'name'=>'james',  
                   'age'=>30,  
                   ),  
           'new'=>array(  
                   'name'=>'kube',  
                   'age'=>37,  
                   ),  
           'list'=>array(  
                   'name'=>'kube',  
                   'age'=>27,  
                   ),  
           '435'=>array(  
                   'name'=>'susu',  
                   'age'=>26,  
                   ),
            '33'=>array(  
                   'name'=>'james',  
                   'age'=>30,  
                   ), 
           );

After calling the function, it is displayed:

Array
(
    [0] => Array
        (
            [name] => james
            [age] => 30
        )
    [1] => Array
        (
            [name] => susu
            [age] => 26
        )
    [new] => Array
        (
            [name] => kube
            [age] => 37
        )
    [list] => Array
        (
            [name] => kube
            [age] => 27
        )
)

Test The demo is as follows:

");   //输出原来的数组  
print_r($arr);  
echo "

"; function more_array_unique($arr=array()){ foreach($arr[0] as $k => $v){ $arr_inner_key[]= $k; //先把二维数组中的内层数组的键值记录在在一维数组中 } foreach ($arr as $k => $v){ $v =join(",",$v); //降维 用implode()也行 $temp[$k] =$v; //保留原来的键值 $temp[]即为不保留原来键值 } $temp =array_unique($temp); //去重:去掉重复的字符串 foreach ($temp as $k => $v){ $a = explode(",",$v); //拆分后的重组 如:Array( [0] => james [1] => 30 ) $arr_after[$k]= array_combine($arr_inner_key,$a); //将原来的键与值重新合并 } return $arr_after; } $arr_new = more_array_unique($arr); //调用去重函数 printf("去除重复元素后的数组:
"); echo '
';
    print_r($arr_new);  
    echo "

"; ?>

Recommended learning: "PHP Video Tutorial"

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