php多维数组去重
程序员文章站
2022-05-31 12:09:49
...
php多维数组去重,怎样去掉name为a的多余的那一个呢?
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
)
)
只是提供新思路 效率不高 勿用
如果只是ID重复,name不重复怎么去掉?
查询的时候能直接把重复的过滤掉吗?
这是我的SQL,我怎么改都改不对,麻烦你帮我改改吧,谢谢了
$sql = "select t3.width as width, t3.height as height,t1.add_time as add_time,t1.add_author as add_author,t1.image_url as image_url,t1.id as id,t1.browse_real_cnt as browse_real_cnt from comments as t2 left join ornamentations as t1 on t1.id = t2.object_id left join ornamentation_images as t3 on t3.ornamentation_id = t1.id where ( t2.uid = $id ) AND ( t1.enable = '0' ) ORDER BY id desc LIMIT 0,5 ";
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
)
)
回复讨论(解决方案)
$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 ))
如果只是ID重复,name不重复怎么去掉?
那你就有 #2 的代码
不过严格的说这不是“去重”而是“聚类”
只不过是对同组数据只取一个,有点数据库中 group 的味道
那你就有 #2 的代码
不过严格的说这不是“去重”而是“聚类”
只不过是对同组数据只取一个,有点数据库中 group 的味道
查询的时候能直接把重复的过滤掉吗?
SELECT DISTINCT(`id`) AS `id` , name FROM `TABLE`
SELECT DISTINCT(`id`) AS `id` , name FROM `TABLE`
这是我的SQL,我怎么改都改不对,麻烦你帮我改改吧,谢谢了
$sql = "select t3.width as width, t3.height as height,t1.add_time as add_time,t1.add_author as add_author,t1.image_url as image_url,t1.id as id,t1.browse_real_cnt as browse_real_cnt from comments as t2 left join ornamentations as t1 on t1.id = t2.object_id left join ornamentation_images as t3 on t3.ornamentation_id = t1.id where ( t2.uid = $id ) AND ( t1.enable = '0' ) ORDER BY id desc LIMIT 0,5 ";
你哪个字段有重复的啊?
上一篇: RMAN完全备份与增量备份