2012-02-17 61 views
1

我有兩個一維數組:兩個一維數組和搜索

$table1 = (9001, 9002, 9003, 9004, 9005, 9006, 9007); 

$table2 = (9001, 9004, 9010); 

我一定要知道,如果從$表2中的任何元素(或沒有)$表1中。

怎麼辦?

回答

9

使用array_intersect()

$array1 = array(9001, 9002, 9003, 9004, 9005, 9006, 9007); 
$array2 = array(9001, 9004, 9010); 
$result = array_intersect($array1, $array2); 

輸出

Array 
(
    [0] => 9001 
    [3] => 9004 
) 
+0

+1打我 – 2012-02-17 19:41:34

+0

感謝你的幫助 – Andrew 2012-02-17 19:56:23

2

使用array_intersect。它返回兩個數組中的值的數組。

$match = array_intersect($table1, $table2); 
+0

感謝你的幫助 – Andrew 2012-02-17 19:56:32

+0

沒問題。 :-) – 2012-02-17 19:57:56