0

How can I unset the keys in one array where the values contained in a second array match the values in the first array?


Actual array:

$fruits = array('Banana','Cherry','Orange','Apple');

Elements I want to remove:

$remove = array('Banana','Apple');

Need to return:

$array = array('Cherry','Orange');

I know it's possible to remove each one with unset, but I'm looking to make it in one line with two array.

Thanks.

3
  • what do you mean by in one line with 2 arrays ? Commented Nov 23, 2014 at 14:33
  • I do not want to have 2 or more unset(...). Commented Nov 23, 2014 at 14:35
  • is not the answer of @tiganion what you are looking for ? Commented Nov 23, 2014 at 14:36

1 Answer 1

3

Take a look at this function link

$arrayWithoutTheDesiredElements = array_diff($originalArr, $toRemoveArray)

EDIT:

for your case: $array = array_diff($fruits, $remove);

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

Comments

Your Answer

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