2017-03-11 99 views
1

如何從多個數組中得到最終的唯一數組結果?如何合併子數組值並生成唯一值的一維數組?

我有一個這樣的數組:

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?

回答

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

這種方式

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

這需要三個核心PHP函數,排序array_merg,和array_unique

排序 - 排序的陣列通過引用發送的,意思而不是返回一個變量,它的變化數組本身的順序。

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裏面。在: https://eval.in/752793

+0

這乾淨呃比接受的答案。 PLUS1 – mickmackusa

0

方法#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:分配臨時密鑰強制覆蓋值以確保沒有重複(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\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)