Reputation: 2437
I have two arrays:
$array1 = Array ( [0] => stdClass Object ( [user_id] => 4 ) [1] => stdClass Object ( [user_id] => 5 ) )
$array2 = Array ( [0] => stdClass Object ( [usr_id] => 4 ) [1] => stdClass Object ( [usr_id] => 5 ) )
I want to check, all elements of $array2
is present in $array1
or not.
How can I do that?
I have searched, but couldn't find a suitable solution for my array.
Note: I want this comparison, without changing the array format.
Upvotes: 1
Views: 87
Reputation: 2437
$checking = (count(array_intersect($arr2, $arr1)) == count($arr2))?1:0;
If all elements of $arr2
is present in $arr1
then it will return 1.
Upvotes: 0
Reputation: 755
$contains = count(array_intersect($array2, $array1)) == count($array2);
Upvotes: 0
Reputation: 3425
Try this:
$fullyExists = (count($array2) == count(array_intersect($array2, $array1));
Upvotes: 1
Reputation: 2048
I don't know it there is a PHP function for that, but you can do your own:
function arrayIsIncluded($array1,$array2){
foreach($array2 as $key => $value){
if (!in_array($value,$array1)){
return false;
}
}
return true;
}
Upvotes: 1