如何在php中获取匹配的数组值

时间:2013-06-12 10:43:32

标签: php

Q1:如何显示匹配值?

阵列1:

Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 [4] => 13 [5] => 14 [6] => 15 [7] => 16 ) 

阵列2:

Array ( [0] => 8 [1] => 9 [2] => 10 [3] => 11 [4] => 12 [5] => 13 [6] => 14 [7] => 15 [8] => 16 [9] => 17 [10] => 18 [11] => 19 [12] => 20 [13] => 21 [14] => 22 [15] => 23 )

输出:

09-16

如何在数组中显示匹配的第一个值和最后一个值

3 个答案:

答案 0 :(得分:1)

$matching = array_intersect($array1, $array2);
print_r($matching);

array_intersect

答案 1 :(得分:0)

获取数组中的匹配值

使用array_intersect()返回匹配的值。

请参阅文档here

示例:

// Get the matching values
$matching_values = array_intersect($array1,$array2);

// This will print the matching values in the arrays
print_r($matching_values);

获取数组中的不匹配值

使用array_diff()返回数组的差异。

请参阅文档here

示例:

// Get the non-matching values
$differences = array_diff($array1,$array2);

// This will print the differences in the arrays
print_r($differences);

答案 2 :(得分:0)

尝试

$arr = array_intersect(array1,array2);
print_r($arr);