43

I have two arrays: Array ( [0] => 2 [1] => 3 ) and Array ( [0] => 2 ).

I want to get the value, which is not in second array. So I have used the array_diff function but my result will get Array ( [1] => 3 ) Actually this is the result. But a small problem here, its position is (key) 1. I want the result in to a new array starts from 0th position, i.e., Array ( [0] => 3 ).

How can I achieve this?

1
  • Could you explain why having the original keys in place causes a problem? Commented Jun 24, 2010 at 12:45

2 Answers 2

88

you can use array_values(array_diff($arr1, $arr2)); if order doesn't matter

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

Comments

22

You should run array_values() on the result and this would give you a new array with indexes starting at 0.

This is a known shortcoming of array_diff(), check the php docs.

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.