Search code examples
phparraysduplicate-data

Comparing arrays with identical values


$array1 = array(1,1,1);  
$array2= array(1,5,9,2,2,1);

I need to compare $array2 with $array1 and if $array2 has the same identical values should return true, otherwise should be returning false. In this case it should return false


Solution

  • Well thanks @Shadowfax for trying help but i made the solution, so i post here if anyone have the same problem..

    function compareArrayValues($array1,$array2){
    $result= array();
    for ($a=0; $a< count($array1); $a++){
        $array2=array_values($array2);
        for ($b=0; $b < count($array2) ; $b++) { 
            if ($array1[$a] == $array2[$b]){
                array_push($result,$array1[$a]);
                unset($array2[$b]);
                break;
            }
        }
    
    }
    if ($result == $array1){
        return true;
    }else{
        return false;
    }
    }