Home >php教程 >PHP源码 >php数组去重

php数组去重

PHP中文网
PHP中文网Original
2016-06-01 14:33:261051browse

php代码

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