数组判重并更新重复值

一维数组:

$arr = [1,2,3,4,5,4,3,2,1,1,1,1];
updateRepeat($arr);

 

处理结果:

array(12) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(4) [4]=> int(5) [5]=> string(4) "4(1)" [6]=> string(4) "3(1)" [7]=> string(4) "2(1)" [8]=> string(4) "1(1)" [9]=> string(4) "1(2)" [10]=> string(4) "1(3)" [11]=> string(4) "1(4)" }

 

二维数组:

   $arr = [
            ['name' =>'233','sex' => 1],
            ['name' =>'456'],
            ['name' =>'hahaha'],
            ['name' =>'qwe'],
            ['name' =>'965'],
            ['name' =>'2333'],
            ['name' =>'233','num' => 2],
            ['name' =>'hahaha'],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1],
            ['name' =>'233','sex' => 1]
     ];
  
updateRepeat($arr,'name');

 

处理结果:

array(12) { [0]=> array(2) { ["name"]=> string(3) "233" ["sex"]=> int(1) } [1]=> array(1) { ["name"]=> string(3) "456" } [2]=> array(1) { ["name"]=> string(6) "hahaha" } [3]=> array(1) { ["name"]=> string(3) "qwe" } [4]=> array(1) { ["name"]=> string(3) "965" } [5]=> array(1) { ["name"]=> string(4) "2333" } [6]=> array(2) { ["name"]=> string(6) "233(1)" ["num"]=> int(2) } [7]=> array(1) { ["name"]=> string(9) "hahaha(1)" } [8]=> array(2) { ["name"]=> string(6) "233(2)" ["sex"]=> int(1) } [9]=> array(2) { ["name"]=> string(6) "233(3)" ["sex"]=> int(1) } [10]=> array(2) { ["name"]=> string(6) "233(4)" ["sex"]=> int(1) } [11]=> array(2) { ["name"]=> string(6) "233(5)" ["sex"]=> int(1) } }

 

代码:

    //二维数组指定字段,更改重复的值
    public function updateRepeat($arr,$field)
    {
        $newarr = array();
        foreach($arr as $k => $v)
        {
            if(in_array($v[$field], $newarr, true))
            {
                $newvalue = $this->updateField($newarr,$v[$field],$v[$field]);
                //将重复的值修改后的值更新到原数组
                $arr[$k][$field] = $newvalue;
                //将重复的值修改后的值放入判重数组
                $newarr[$k] = $newvalue;
            } else {
                //将不重复的值放入判重数组
                $newarr[$k] = $v[$field];
            }
        }
        return $arr;
    }

    //一维数组更改重复的值
    public function updateRepeat1($arr)
    {
        $newarr = array();
        foreach($arr as $k => $v)
        {
            if(in_array($v, $newarr, true))
            {
                $newvalue = $this->updateField($newarr,$v,$v);
                //将重复的值修改后的值更新到原数组
                $arr[$k] = $newvalue;
                //将重复的值修改后的值放入判重数组
                $newarr[$k] = $newvalue;
            } else {
                //将不重复的值放入判重数组
                $newarr[$k] = $v;
            }
        }
        return $arr;
    }
    //处理重复值
    public function updateField($arr,$oldv,$newv,$num = 1)
    {
        //为了实现递归所以重新判重
        if(in_array($newv, $arr, true))
        {
            $newvalue = $oldv.'('.$num.')';
            $newnum = $num + 1;
            $res = $this->updateField($arr,$oldv,$newvalue,$newnum);
        } else {
            return $newv;
        }
        return $res;
    }

一个简单的数组判重,原理是将数组遍历,依次取出值,每取到一个值就和之前取到的值的集合进行匹配,如果有重复了就更新值。

注意in_array()第三个参数,默认false使用的 == 匹配,true使用 === 匹配,如果不使用严格匹配,值为整型时会判断错误。

posted @ 2019-12-23 11:56  一剑还  阅读(344)  评论(1编辑  收藏  举报