PHP比较两个数组并获得匹配值没有区别 [英] PHP compare two arrays and get the matched values not the difference
本文介绍了PHP比较两个数组并获得匹配值没有区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图比较两个数组,只获得两个数组上存在的值,但不幸的是,我找不到正确的数组函数使用...
我发现 array_diff()
函数: http://php.net/manual/en/function.array-diff.php
但这是为了两个数组的区别。 / p>
示例:
$ array1 =数组(** alpha **,omega,** bravo **,** charlie **,** delta **,** foxfrot **);
$ array2 = array(** alpha **,gamma,** bravo **,x-ray,** charlie **,** delta ** ,halo,eagle,** foxfrot **);
预期输出:
$ result = array(** alpha **,** bravo **,** charlie **,** delta ** ** foxfrot **);
解决方案
/www.php.net/manual/en/function.array-intersect.php> array_intersect()
:
$ result = array_intersect($ array1,$ array2);
I'm trying to compare two arrays and get only the values that exist on both arrays but, unfortunately, I can't find the right array function to use...
I found the array_diff()
function: http://php.net/manual/en/function.array-diff.php
But it's for the difference of the both arrays.
Example:
$array1 = array("**alpha**","omega","**bravo**","**charlie**","**delta**","**foxfrot**");
$array2 = array("**alpha**","gamma","**bravo**","x-ray","**charlie**","**delta**","halo","eagle","**foxfrot**");
Expected Output:
$result = array("**alpha**","**bravo**","**charlie**","**delta**","**foxfrot**");
解决方案
Simple, use array_intersect()
instead:
$result = array_intersect($array1, $array2);
这篇关于PHP比较两个数组并获得匹配值没有区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文