如何从多个数组中获取最终的唯一数组结果?
我有一个这样的数组:
Array
(
[0] => Array
(
[0] => 8
[1] => 9
[2] => 7
)
[1] => Array
(
[0] => 7
[1] => 8
[2] => 9
[3] => 33
[4] => 21
)
[2] => Array
(
[0] => 11
[1] => 12
[2] => 33
[3] => 21
[4] => 9
[5] => 31
)
)
预期结果:
Array(
[0] => 7
[1] => 8
[2] => 9
[3] => 33
[4] => 21
[5] => 11
[6] => 12
[7] => 31
)
如何使用php做到这一点?
答案 0 :(得分:2)
在您想要的输出索引相同的情况下,您永远无法实现。因为最近的值会覆盖相同的索引。
您可以如下所示: -
$final_array = array_unique(call_user_func_array('array_merge', $array)); //convert multi-dimensional array to single dimensional and remove duplicates
asort($final_array); // sort by value. this is optional
$final_array = array_values($final_array); // re-index final array and this is optional too
echo "<pre/>";print_r($final_array); // print final array
输出: - https://eval.in/752750
答案 1 :(得分:2)
这需要三个核心PHP函数,排序, array_merg 和 array_unique :
sort - 对通过引用发送的数组进行排序,这意味着不是返回变量,而是更改数组本身的顺序。
array_merg - 当与call_user_func_array结合使用时,会将所有数组动态组合在一起,无论多少都有。
array_unique - 确保每个元素只有一个。
int compatible[num_terms][num_terms];
// initialize every cell to 0
for (size_t i = 0; i < num_terms; i++) {
for (size_t j = 0; j < num_terms; j++) {
printf("writing 0 to (%zu, %zu)\n", i, j);
compatible[i][j] = 0;
}
}
输出:
<?php
$arr = [ [8,9,7], [7,8,9,33,21], [11,12,33,21,9,31] ];
$merged = array_unique(call_user_func_array('array_merge', $arr));
sort($merged);
print_r($merged);
?>
这里是eval.in里面的: https://eval.in/752793
答案 2 :(得分:1)
这样
<?php
$arr = [ [8,9,7], [7,8,9,33,21], [11,12,33,21,9,31] ];
$final = array();
foreach($arr as $child){
foreach($child as $value){
$final[] = $value;
}
}
$final = array_unique($final);
print_r($final);
?>
输出:
Array
(
[0] => 8
[1] => 9
[2] => 7
[6] => 33
[7] => 21
[8] => 11
[9] => 12
[13] => 31
)
答案 3 :(得分:0)
方法#1 :foreach
循环isset()
,按第一次出现的值排序值(Demo)
(*此方法似乎是最快的)
$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
foreach($array as $sub){
foreach($sub as $v){
if(!isset($result[$v])){ // only add first occurence of a value
$result[$v]=$v;
}
}
}
var_export(array_values($result)); // re-index and print to screen
// condensed output: array(8,9,7,33,21,11,12,31)
方法#2 :分配强制值覆盖的临时密钥以确保没有重复(Demo)
$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
foreach($array as $sub){
foreach($sub as $v){
$result[$v]=$v; // force overwrite because duplicate keys cannot occur
}
}
sort($result); // sort and re-index
var_export($result); // print to screen
// condensed output: array(7,8,9,11,12,21,31,33)
方法#3 :array_merge()
splat operator
和array_unique()
(Demo)
$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
$unique=array_unique(array_merge(...$array)); // merge all subarrays
sort($unique); // sort and re-index
var_export($unique); // print to screen
// condensed output: array(7,8,9,11,12,21,31,33)
方法#4 :非正统 json_encode()
&amp; preg_match_all()
(Demo)(Pattern Demo)
$array=[[8,9,7],[7,8,9,33,21],[11,12,33,21,9,31]];
$unique=preg_match_all('~\b(\d+)\b(?!.*\b\1\b)~',json_encode($array),$out)?$out[0]:[];
sort($unique); // sort and re-index
var_export($unique); // print to screen
// condensed output: array(7,8,9,11,12,21,31,33)