首页 >php教程 >PHP源码 >php数组去重

php数组去重

PHP中文网
PHP中文网原创
2016-06-01 14:33:261053浏览

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);
?>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn