2016-09-29 60 views
-1

如何從兩個數組中獲得唯一的月份? 我有兩個數組:如何在foreach循環中從兩個數組中獲得唯一的月份

$ar1 =Array 
(
[0] => Array 
    (
     [0] => 1 
     [month] => 1 
     [1] => -40964.49999999999 
     [total] => -40964.49999999999 
    ) 

[1] => Array 
    (
     [0] => 2 
     [month] => 2 
     [1] => -41353.93 
     [total] => -41353.93 
    ) 

[2] => Array 
    (
     [0] => 3 
     [month] => 3 
     [1] => -32716.35 
     [total] => -32716.35 
    ) 

[3] => Array 
    (
     [0] => 4 
     [month] => 4 
     [1] => -30399.059999999998 
     [total] => -30399.059999999998 
    ) 

[4] => Array 
    (
     [0] => 5 
     [month] => 5 
     [1] => -33781.689999999995 
     [total] => -33781.689999999995 
    ) 

[5] => Array 
    (
     [0] => 6 
     [month] => 6 
     [1] => -26129.219999999994 
     [total] => -26129.219999999994 
    ) 

[6] => Array 
    (
     [0] => 7 
     [month] => 7 
     [1] => -40643.990000000005 
     [total] => -40643.990000000005 
    ) 

[7] => Array 
    (
     [0] => 8 
     [month] => 8 
     [1] => -63260.61 
     [total] => -63260.61 
    ) 

) 
$ar2 = Array 
(
[0] => Array 
    (
     [0] => 1 
     [month] => 1 
     [1] => 40616.1 
     [total] => 40616.1 
    ) 

[1] => Array 
    (
     [0] => 2 
     [month] => 2 
     [1] => 39640.659999999996 
     [total] => 39640.659999999996 
    ) 

[2] => Array 
    (
     [0] => 3 
     [month] => 3 
     [1] => 34639.73 
     [total] => 34639.73 
    ) 

[3] => Array 
    (
     [0] => 4 
     [month] => 4 
     [1] => 37337.62 
     [total] => 37337.62 
    ) 

[4] => Array 
    (
     [0] => 5 
     [month] => 5 
     [1] => 35776.98000000001 
     [total] => 35776.98000000001 
    ) 

[5] => Array 
    (
     [0] => 6 
     [month] => 6 
     [1] => 35001.97 
     [total] => 35001.97 
    ) 

[6] => Array 
    (
     [0] => 7 
     [month] => 7 
     [1] => 32456.749999999996 
     [total] => 32456.749999999996 
    ) 

[7] => Array 
    (
     [0] => 8 
     [month] => 8 
     [1] => 41992.56999999999 
     [total] => 41992.56999999999 
    ) 

[8] => Array 
    (
     [0] => 9 
     [month] => 9 
     [1] => 238.18 
     [total] => 238.18 
    ) 

[9] => Array 
    (
     [0] => 10 
     [month] => 10 
     [1] => 238.22 
     [total] => 238.22 
    ) 

) 

我怎樣才能獲得獨特月份從兩個陣列?

+0

http://php.net/manual/en/function.array-unique.php –

+0

@AnkitShah返回數組中的唯一值,而不是來自兩個數組的唯一值,即array1中的值不在array2中 –

回答

1

您可以從兩個陣列存儲所有月份單陣列後重函數array_unique():

$arr_tmp = []; 
$res = []; 
foreach($ar1 as $a1) { 
    $arr_tmp[] = $a1['month']; 
} 
foreach($ar2 as $a2 { 
    $arr_tmp[] = $a2['month']; 
} 
$res = array_unique($arr_tmp); 
+0

感謝重播,它實際上是我想要的。感謝幫助隊友:) –

相關問題