I have an issue, I have this array
$items= array('ABC','DEF',GHI');
and have another two arrays
$array1 = array('ABC','DEF',GHI');
$array2 = array('DEF');
$array1 should return TRUE
because all elements are in $items
$array2 should return FALSE
because 'ABC' and 'GHI' arent in that array
i've tried with array_intersect
and array_diff
but i cant get it,
$result = array_intersect($items,$array1);
$result = !array_diff($items,$array1);
Could you please help me? Regards Mario
CodePudding user response:
To see if the array contains at least all values in $items
, just get all values from the array that are also in $items
and compare it with $items
:
$result = (array_intersect($array1, $items) == $items);
If you just need to compare the arrays:
$result = ($array1 == $items);
But why is this not working for you:
$result = !array_diff($items, $array1);
CodePudding user response:
If you dont want to use array_diff, you can use the multidimensional arrays.
Put your arrays inside another array:
$items1= array(
array(0=>"ABC", 1=> "DEF", 2=>"GHI"),
array(0=>"ABC", 1=> "DEF", 2=>"GHI"),
array(0=>"DEF"),
);
Check them with conditional statements:
if($items1[0]==$items1[1]){
echo "true";
}if($items1[1]==$items1[2]){
echo "true 2";
}else{
echo "false";
}