在php中比较两个数组并输出结果[重复]
问题描述:
Possible Duplicate:
PHP compare array
I have to compare two arrays in php and print the out put if the both arrays are same but can order elements in any way
ie
$array1=array('a','p','p','l','e');
$array2=array('p','a','e','l');
--- This must return as success because all of the letters in array1 is there in array2
$array1=array('a','p','p','l','e','s');
$array2=array('p','a','e','l');
-- This must return false
$array1=array('a','p','p','l','e','s');
$array1=array('a','p','p','l','e','s');
-- This must return true
Please help
可能重复: strong>
PHP比较数组 p> blockquote>我有 比较php中的两个数组并打印输出,如果两个数组相同但可以以任何方式排序元素 p>
即 p>
$ ARRAY1 =阵列( 'A', 'p', 'p', 'L', 'E'); $的数组2 =阵列( 'p', 'A', 'E', 'L'); ---这必须返回成功,因为array1中的所有字母都在array2 $ array1 = array('a','p','p','l','e)中 ','s'); $ array2 = array('p','a','e','l'); --这必须返回false $ array1 = array( 'A', 'p', 'p', 'L', 'E', 'S'); $的ARRAY1 =阵列( 'A', 'p', 'p', 'L','E ','s'); --这必须返回true code> pre>
请帮助 p> div>
答
var_dump(sizeof(array_diff($array1, $array2)) === 0);
答
function compareArrays($array1, $array2) {
foreach ($array2 as $currentValue) {
if (!in_array($currentValue, $array1) {
return false;
}
}
return true;
}