php从两个数组中获取匹配值

时间:2014-12-11 09:50:33

标签: php arrays arraylist

我有两个像这样的阵列

$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish [4] => Hindi );

我希望两个数组中都存在相同的值。所以为此,我使用了array_intersect这样的

<?php
$array1 = Array ( [0] => Dutch [1] => Kannada [2] => Vietnamese [3] => Belarusian );
$array2 = Array ( [0] => English [1] => Germany [2] => Vietnamese [3] => Spanish )
$result = array_intersect($array1, $array2);
print_r($result);
?>

但它没有显示匹配的值。即使它显示错误如

Parse error: syntax error, unexpected '[', expecting ')'

我认为数组代码结构很好,为什么我收到此错误而没有从两个数组中获取匹配值?任何帮助和建议都会非常明显。感谢

2 个答案:

答案 0 :(得分:3)

这应该适合你:

(首先你宣布你的数组错了,忘记了;

$array1 = array ("Dutch", "Kannada", "Vietnamese", "Belarusian");
$array2 = array ("English", "Germany", "Vietnamese", "Spanish");
$result = array_intersect($array1, $array2);
print_r($result);

输出:

Array ( [2] => Vietnamese )

答案 1 :(得分:0)

试试这个:

<?php 

$array1 = Array ( '0' => 'Dutch', '1' => 'Kannada', '2' => 'Vietnamese', '3' => 'Belarusian' );
$array2 = Array ( '0' => 'English', '1' => 'Germany','2' => 'Vietnamese', '3' => 'Spanish' );
$result = array_intersect($array1, $array2);
print_r($result);
?>

输出:Array ( [2] => Vietnamese )