$arr = array( '0' => array( 'name' => 'james', 'age' => 30, ), '1' => array( 'name' => 'susu', 'age' => 26, ), '2' => array( 'name' => 'james', 'age' => 30, ), '3' => array( 'name' => 'kube', 'age' => 37, ), '4' => array( 'name' => 'kube', 'age' => 27, ), ); $key = "name"; function second_array_unique_bykey($arr, $key) { $tmp_arr = array(); foreach ($arr as $k => $v) { if (in_array($v[$key], $tmp_arr)) //搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true { unset($arr[$k]); //销毁一个变量 如果$tmp_arr中已存在相同的值就删除该值 } else { $tmp_arr[$k] = $v[$key]; //将不同的值放在该数组中保存 } } //ksort($arr); //ksort函数对数组进行排序(保留原键值key) sort为不保留key值 return $arr; } $result = second_array_unique_bykey($arr, $key); echo "<pre>"; print_r($result);
https://www.cnblogs.com/T8888/p/12929615.html
标签:tmp,arr,name,age,二维,键值,key,array,PHP From: https://www.cnblogs.com/xiaoyantongxue/p/16626624.html