如何合并子数组值并生成唯一值的一维数组?

How to merge subarray values and generate a 1-dimensional array of unique values?

如何从多个数组中获取最终的唯一数组结果?

我有一个这样的数组:

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 做到这一点?

在你想要的输出索引是相同的,你永远无法实现。因为相同的索引被最近的值覆盖。

你可以得到如下:-

$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

这样

<?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);
?>

演示:https://eval.in/752766

输出:

Array
(
    [0] => 8
    [1] => 9
    [2] => 7
    [6] => 33
    [7] => 21
    [8] => 11
    [9] => 12
    [13] => 31
)

这需要三个核心 PHP 函数,sortarray_merg array_unique:

sort - 对通过引用发送的数组进行排序,这意味着它不是返回变量,而是更改数组本身的顺序。

array_merg - 当与 call_user_func_array 组合时将动态地将所有数组组合在一起,无论有多少。

array_unique - 确保每个元素只有一个。

<?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);
?>

输出:

Array
(
    [0] => 7
    [1] => 8
    [2] => 9
    [3] => 11
    [4] => 12
    [5] => 21
    [6] => 31
    [7] => 33
)

这是 eval.in 里面的: https://eval.in/752793

方法 #1foreach 循环 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:分配强制 value-overwriting 以确保没有重复的临时密钥 (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) 

方法 #3array_merge()splat operatorarray_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() & 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\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)