Home >Backend Development >PHP Tutorial >php数组去重的函数代码_PHP

php数组去重的函数代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:08:38759browse
复制代码 代码如下:
    function assoc_unique($arr, $key) {  
        $tmp_arr = array();  
        foreach($arr as $k => $v) {  
            if(in_array($v[$key], $tmp_arr)) {  
                unset($arr[$k]);  
            } else {  
                $tmp_arr[] = $v[$key];  
            }  
        }  
        sort($arr);  
        return $arr;  
    }  

    $aa = array(  
        array('id' => 123, 'name' => '张三'),  
        array('id' => 123, 'name' => '李四'),  
        array('id' => 124, 'name' => '王五'),  
        array('id' => 125, 'name' => '赵六'),  
        array('id' => 126, 'name' => '赵六')  
    );  
    $key = 'name';  
    assoc_unique(&$aa, $key);  
    print_r($aa); 
?>
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