Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Here are the arrays.

Array1
(
[439] => dsaffsdfdfdfsffdsf
[379] => 3454
[375] => 3 ///why is it removed in the resulting array?
[436] => Fdsafdfsdf
[432] => 3  /// this one too
[431] => 2
[385] => 499
[434] => 3501
[435] => 2013-01-16
[430] => 1
[440] => fsdzsdaffdsfffdsf
[406] => YES
[438] => 32442344324324234
[376] => 3
)

Array2
(
[376] => 3
[385] => 499
)

array_diff(array1, array2)

result
(
[439] => dsaffsdfdfdfsffdsf
[379] => 3454
[436] => Fdsafdfsdf
[431] => 2
[434] => 3501
[435] => 2013-01-16
[430] => 1
[440] => fsdzsdaffdsfffdsf
[406] => YES
[438] => 32442344324324234
)

Looks weird to me. Any idea? Its removing based on value?

I just want Array2 elements removed from Array1

share|improve this question
4  
RTFM :) array_diff - Returns an array containing all the entries from array1 that are not present in any of the other arrays. –  Peter Jan 16 '13 at 23:42
1  
It's removing based on value, and the documentation says so. There's even an example that shows exactly this behavior. Perhaps you are looking for array_diff_assoc. –  Jon Jan 16 '13 at 23:44
 
Hmmm.. I didn't think so. I will try the assoc one. Thanks! –  Kevin Rave Jan 16 '13 at 23:44
2  
@KevinRave: Please don't try the assoc version. Just read the docs and decide if it does what you want. Programming is not randomly typing stuff until something works. –  Jon Jan 16 '13 at 23:45
 
I got it, @Jon. Thanks! –  Kevin Rave Jan 16 '13 at 23:48
add comment

1 Answer

up vote 0 down vote accepted

Manual is your best friend:

  • array_diff() Compares array1 against array2 and returns the difference.
  • array_diff_assoc() Compares array1 against array2 and returns the difference. Unlike array_diff() the array keys are also used in the comparison.
  • array_diff_key() Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.