php 二维数组根据某个key去重

function arrayUnset($arr, $key)
 {
        $newArr = [];
        foreach ($arr as $value) {
            if (isset($newArr[$value[$key]])) { //查看有没有重复项
                unset($value[$key]);
            } else {
                $newArr[$value[$key]] = $value;
            }
        }
        return array_values($newArr);
}

function test()
{
        $arr = [
            ['title' => '1111', 'date' => 'ddddd'],
            ['title' => '2222', 'date' => 'fffffff'],
            ['title' => '1111', 'date' => 'ggggggg']
        ];

        $newArr = $this->arrayUnset($arr, 'title');
        var_dump($newArr);
}

 

posted @ 2021-02-20 10:49  pengcx  阅读(27)  评论(0编辑  收藏  举报