2012-06-08 76 views
3

好了,兩個數組:對面陣列以及PHP合併

$first = array(1,2,3,4,5,6,7,8,9,10); 
$second = array(1,2,3,4,5); 

有沒有一種方法(不通過他們循環,如果它可以幫助)做一個array_merge風格功能,其中它返回數組:

$new = array(6,7,8,9,10); 

如果發現匹配,則不返回匹配項。

+1

和array_diff()可能? –

回答

5

array_diff()

$new = array_diff($first, $second); 
print_r($new); 

/* 
Array 
(
    [5] => 6 
    [6] => 7 
    [7] => 8 
    [8] => 9 
    [9] => 10 
) 
*/ 
2

array_diff()應該這樣做:

<?php 
$array1 = array("a" => "green", "red", "blue", "red"); 
$array2 = array("b" => "green", "yellow", "red"); 
$result = array_diff($array1, $array2); 

print_r($result); 
?> 

Array 
(
    [1] => blue 
) 
0

試試這個:

$new = array_diff($first , $second);