PHP : How can I check Array in array?

109 views Asked by At

I have 1 array, and the this array contains is an array also for each element. ex:

$arraycenter = array(
                      array('a','b','c','d'),  //array1
                      array('e','d','f','g'),  //array2
                      array('a','b','c','d'),  //array3
                      array('e','d','f','g'),   //array4
                      array(.............. ),  //.....
                      array(...............);  //array++

How can check array1 & array3 is the same, and array2 & array 4 is the same?

2

There are 2 answers

3
Fohlen On BEST ANSWER

You can use array_diff

if (array_diff($arraycenter[1], $arraycenter[2], $arraycenter[3]))
    // Do something

Or (if you don't know how many items need to be checked) you can use array_filter

$array_center = array_filter($array_center, function($value) use(&$array_center) 
 {
    return array_diff($array_center[1], $value);
 }
6
Mureinik On

You can use the === operator:

if ($arraycenter[0] === $arraycenter[2] && $arraycenter[1] === $arraycenter[3]) {
    // do stuff...
}