0

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

Community
  • 1
  • 1
ramesh
  • 4,008
  • 13
  • 72
  • 117

2 Answers2

5
var_dump(sizeof(array_diff($array1, $array2)) === 0);

ref: http://php.net/manual/function.array-diff.php

Yoshi
  • 54,081
  • 14
  • 89
  • 103
1
function compareArrays($array1, $array2) {
    foreach ($array2 as $currentValue) {
        if (!in_array($currentValue, $array1) {
            return false;
        }
    }
    return true;
}
davidethell
  • 11,708
  • 6
  • 43
  • 63