Frage

$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

War es hilfreich?

Lösung 3

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;
}
}

Andere Tipps

You can use the == and === operators.

$array1 == $array2 only checks if the two arrays contain the same key/value pairs and $array1 === $array2 also checks if they are in the same order and if they are of the same type.

See the PHP manual.

if ( $array1 == $array2 ) {
    return true;
}
else{
    return false;
}

Note: keys must be same too.


To check only values:

if(!array_diff($array1, $array2) && !array_diff($array2, $array1)) 
   return true;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top