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.

This is such a simple problem but the PHP doc does not explain why it is happening.

I have this code:

var_dump($newattributes); var_dump($oldattributes);
var_dump(array_diff($newattributes, $oldattributes));

For briefity I am going to omit large parts of the structure I am actually using (since each is 117 elements long) and cut to the case.

I have one array called $newattributes which looks like:

array(117){
    // Lots of other attributes here
    ["deleted"] => int(1)
}

And another called $oldattributes which looks like:

array(117){
    // Lots of other attributes here
    ["deleted"] => string(1) "0"
}

Which looks different right? According to array_diff: no. The output I get from array_diff is:

array(0) { } 

I have read the documentation page however it says:

Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.

And I am not sure how "1" can object equal "0".

So am I seeing some caveat with array_diff I didn't take into consideration?

share|improve this question
2  
Good question, clear, well asked and not too long and too much code. More people should do it like this! +1 –  Hidde Aug 17 '12 at 10:41
2  
This shouldn't happen, and indeed it does not for me. –  Jon Aug 17 '12 at 10:42
    
@Jon Thanks for the test, I'll look more into my PHP build –  Sammaye Aug 17 '12 at 10:47

2 Answers 2

up vote 9 down vote accepted

The problem might reside in the fact that you are using associative arrays : you should try and use the following for associative arrays : array_diff_assoc():

<?php 
    $newattributes = array(
       "deleted" => 1 
    );

    $oldattributes = array(
       "deleted" => "0" 
    );

    $result = array_diff_assoc($newattributes, $oldattributes);

    var_dump($result);
?>

result :

   array(1) {
       ["deleted"]=>
       int(1)
   }
share|improve this answer
    
Yea that worked, wow thats just insane –  Sammaye Aug 17 '12 at 10:56
    
you are welcome :) PHP is a very forgiving language, knowing the right functions at times usually does the right trick. –  DonSeba Aug 17 '12 at 10:57
    
+1 . You have awesome presence of mind. Associative arrays! –  KarmicDice Aug 17 '12 at 11:02

It does happen to me too (when there are more values than one)

$new = array('test' => true, 'bla' => 'test' 'deleted' => 1);
$old = array('test' => true, 'deleted' => '0');

For a full array_diff you need to make some extra work, because in default it returns a relative complement

Try this:

array_diff(array_merge($new, $old), array_intersect($new, $old))

Result:

Array
(
    [bla] => test
    [deleted] => 0
)
share|improve this answer
    
Indeed that works thanks, I have taken the other answer as the marked one since it doesn't need the merge or intersect but +1 for being first with a working and backing math, this is prolly what PHP does when comparing assoc –  Sammaye Aug 17 '12 at 10:58
    
That work around does the same as : array_diff_assoc() . great find though :) +1 –  DonSeba Aug 17 '12 at 10:59
    
Yeahh, array_diff_assoc obviously is way easier, didn't know that. SO is a great tool to learn extra things :) –  Dan Lee Aug 17 '12 at 11:54

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.