1
cnqncom OP $arr = array_merge($labels,$label);
//合并需要合并的俩个数组 $key = id;//去重条件 $tmp_arr = array();//声明数组 foreach($arr as $k => $v) { if(in_array($v[$key], $tmp_arr)) //搜索$v[$key]是否在$tmp_arr 数组中存在,若存在返回 true { unset($arr[$k]); //删除掉数组($arr )里相同 ID 的数组 } else { $tmp_arr[] = $v[$key]; //记录已有的 id } } dump($arr); |
2
airdge 2018-05-15 11:13:58 +08:00
用 array_column 提取 foo 作为新数组,新数组去重,获取相应键值就可以了
<?php class str { public static $data = [ [ 'foo' => 'bar', 'x' => 'foo', ], [ 'foo' => 'bar1', 'x' => 'foo1', ], [ 'foo' => 'bar', 'x' => 'foo2', ], [ 'foo' => 'bar2', 'x' => 'foo2', ], [ 'foo' => 'bar2', 'x' => 'foo1', ], [ 'foo' => 'bar3', 'x' => 'foo4', ], ]; public static function a() { return array_map("self::b", array_keys(array_unique(array_column(self::$data, 'foo')))); } public static function b($a) { return self::$data[$a]; } } print_r(str::a()); Array ( [0] => Array ( [foo] => bar [x] => foo ) [1] => Array ( [foo] => bar1 [x] => foo1 ) [2] => Array ( [foo] => bar2 [x] => foo2 ) [3] => Array ( [foo] => bar3 [x] => foo4 ) ) |