开发者

Compare array attributes

开发者 https://www.devze.com 2022-12-08 20:28 出处:网络
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.

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
        (
         开发者_StackOverflow   [idobj:protected] => 2
        )

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

Array 2

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


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.


Are you actually looking for array_intersect()?

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

精彩评论

暂无评论...
验证码 换一张
取 消