30

Lets say I have the following two PHP arrays that contain integers:

$foo = array(1, 5, 9, 14, 23, 31, 45);
$bar = array(14, 31, 36);

I want to remove the items in $foo where the same value exists in $bar

So the result of the process would create a $filteredFoo array that contains:

1, 5, 9, 23, 45

Having looked through the docs on php.net there doesn't seem to be an existing function to perform this kind of action. So is my only option to use foreach and iterate through $foo checking values $bar on each iteration?

0

1 Answer 1

59

You can use array_diff():

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

$filteredFoo = array_diff($foo, $bar);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.