时间:2021-07-01 10:21:17 帮助过:29人阅读
$ar = array ( 0 => array ( 'id' => '79', 'name' => 'a', ), 1 => array ( 'id' => '79', 'name' => 'a', ), 2 => array ( 'id' => '78', 'name' => 'b', ), 3 => array ( 'id' => '36', 'name' => 'c', ),);foreach($ar as $k=>$f) { foreach($ar as $p=>$t) if($k != $p && $f == $t) unset($ar[$k]);}print_r($ar);
Array( [1] => Array ( [id] => 79 [name] => a ) [2] => Array ( [id] => 78 [name] => b ) [3] => Array ( [id] => 36 [name] => c ))
$arr=array( 0=>array( 'id'=>79, 'name'=>'a' ), 1=>array( 'id'=>79, 'name'=>'a' ), 2=>array( 'id'=>78, 'name'=>'b' ), 3=>array( 'id'=>36, 'name'=>'c' ),);$item=array();foreach($arr as $k=>$v){ if(!isset($item[$v['id']])) $item[$v['id']]=$v;}print_r(array_values($item));
Array( [0] => Array ( [id] => 79 [name] => a ) [1] => Array ( [id] => 78 [name] => b ) [2] => Array ( [id] => 36 [name] => c ))
array ( 'id' => '79', 'name' => 'a', ), 1 => array ( 'id' => '79', 'name' => 'a', ), 2 => array ( 'id' => '78', 'name' => 'b', ), 3 => array ( 'id' => '36', 'name' => 'c', ),);$temp =array();$result =array();foreach($ar as $item){ $temp[] = json_encode($item);}$temp = array_unique($temp);foreach($temp as $item){ $result[] = json_decode($item,true);}var_dump($result);
array(3) { [0]=> array(2) { ["id"]=> string(2) "79" ["name"]=> string(1) "a" } [1]=> array(2) { ["id"]=> string(2) "78" ["name"]=> string(1) "b" } [2]=> array(2) { ["id"]=> string(2) "36" ["name"]=> string(1) "c" }}
$ar = array ( 0 => array ( 'id' => '79', 'name' => 'a', ), 1 => array ( 'id' => '79', 'name' => 'a', ), 2 => array ( 'id' => '78', 'name' => 'b', ), 3 => array ( 'id' => '36', 'name' => 'c', ),);foreach($ar as $k=>$f) { foreach($ar as $p=>$t) if($k != $p && $f == $t) unset($ar[$k]);}print_r($ar);
Array( [1] => Array ( [id] => 79 [name] => a ) [2] => Array ( [id] => 78 [name] => b ) [3] => Array ( [id] => 36 [name] => c ))
那你就有 #2 的代码
不过严格的说这不是“去重”而是“聚类”
只不过是对同组数据只取一个,有点数据库中 group 的味道
那你就有 #2 的代码
不过严格的说这不是“去重”而是“聚类”
只不过是对同组数据只取一个,有点数据库中 group 的味道
SELECT DISTINCT(`id`) AS `id` , name FROM `TABLE`
SELECT DISTINCT(`id`) AS `id` , name FROM `TABLE`
你哪个字段有重复的啊?