views:

51

answers:

2

I would like to compare two array items with php, I think I should use array_intersect_key but I don't know how I can do that.

Array 1

 [1] => obj Object
        (
            [idobj:protected] => 2
        )

 [2] => obj Object
        (
            [idobj:protected] => 1
        )

Array 2

 [1] => obj Object
        (
            [idobj:protected] => 1
        )
+2  A: 

No, you don't need to use array_intersect_key() if you need only to compare array elements.

It simple like this (for two-dimensional arrays):

if( $array1[0] == $array2[0] ) {
  echo 'Array items are equal';
} else {
  echo 'Array items are not equal';
}

If you have multi-dimensional array you may need add some extra indexes.

PHP manual has a very good information regarding arrays, check it out.

Andrejs Cainikovs
A: 

Are you actually looking for array_intersect()?

$objectsInArray1ThatArePresentInArray2 = array_intersect($array1, $array2);
soulmerge