0

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

8
  • What if $array1 had other values like 'JKL' would it still be true? Commented Oct 14, 2021 at 17:29
  • that will be false, Commented Oct 14, 2021 at 17:33
  • Well then the arrays need to be exactly the same? Just use == then. Commented Oct 14, 2021 at 17:34
  • All elements of $array2 are in $items. Why is it false? Commented Oct 14, 2021 at 17:35
  • Sort the arrays and then use == if each has to contain all elements of the other. Commented Oct 14, 2021 at 17:35

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

1 Comment

I tried with == but if i change the order to 'DEF','ABC',GHI' that give me false, but it should be true, because it doesn't matter the order,
0

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";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.