4

I want to check if any of the array values from example 1 are in example 2 and remove them from example 2 if they are. How would I be able to do this using PHP?

Example 1

Array
(
    [0] => 3
    [1] => 5
)

Example 2

Array
(
    [0] => 3
    [1] => 3
    [2] => 4
    [3] => 4
    [4] => 4
    [5] => 3
    [6] => 3
    [7] => 3
    [8] => 4
    [9] => 4
    [10] => 4
    [11] => 3
)

3 Answers 3

9

$example2 = array_diff($example2, $example1)

Sign up to request clarification or add additional context in comments.

Comments

7

$array = array_diff($array2, $array1);

array_diff computes the difference between arrays. It returns an array containing all the entries from the first array that are not present in any of the other arrays.

Comments

0
foreach($example2 as $key => $value) {
   foreach($example1 as $key1 => $value1) {
      if ($value1 == $value) {
         unset($example2[$key])
      }
   }
}

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.