4

I have two arrays .
Check the code

$array1 = array(0=>'215',1=> '225');
$array2 = array(0=>'225');
$diff_result = array_diff($array1, $array2);
$diff = array_values($diff_result);
print_r($array1);echo "<br>";
print_r($array2);echo "<br>";
print_r($diff_result);echo "<br>";
print_r($diff);

Now I am getting answers as

Array ( [0] => 215 [1] => 225 )
Array ( [0] => 225 )
Array ( [0] => 215 [1] => 225 )
Array ( [0] => 215 [1] => 225 ) 

But according to array_diff manual it should be

Array ( [0] => 215 [1] => 225 ) 
Array ( [0] => 225 ) 
Array ( [0] => 215 ) 
Array ( [0] => 215 )

What could be the problem

Wazy
  • 8,822
  • 10
  • 53
  • 98
  • 1
    [Works fine for me](http://www.ideone.com/ZCnJh). – Jon Sep 08 '11 at 12:51
  • 1
    i think the answer are the keys. Try it without the keys, use $array1 = array('215', '225'); $array2 = array('225'); – trampi Sep 08 '11 at 12:52
  • 2
    @trampi: What you wrote is exactly the same as the example. And even if it were not, `array_diff` does not care about keys. – Jon Sep 08 '11 at 12:53
  • i think that two arrays are not equal if the one has keys pregiven and the other not. But you are right that array_diff shall not care about keys... – trampi Sep 08 '11 at 12:57
  • trampi I m creating those array dynamically...How can i make array in php without key can you help me out...Please I also think problem is in keys – Wazy Sep 08 '11 at 13:03
  • 1
    @trampi: That's simply incorrect. The keys are consecutive integers starting from 0, they would be the same if auto-populated by PHP. – Jon Sep 08 '11 at 13:25
  • 1
    @WasimKarani: **Two** people have run your code and gotten the correct results (I even give a link). At this point I think that the problem is *not* the keys, but either that you are running something other than the code you give here, or that your system needs a reboot. – Jon Sep 08 '11 at 13:26
  • 1
    Is it a problem with version of Php i am using...Why are you suggesting reboot...My php version is 5.3.5 – Wazy Sep 08 '11 at 13:30
  • I was unable to reproduce this. Tried it on a few different configurations of PHP, getting the same results as the manual. – NullUserException Sep 08 '11 at 13:45
  • When i change the index of first array i m getting correct answer.... – Wazy Sep 08 '11 at 14:15
  • @Jon, I agree. I don't think OP was running the code shown above. It's possible OP was generating the arrays using `file()` which will insert newlines if you don't actively suppress it. I've submitted a change request with PHP to see if they'll make that function do it by default. That's at: https://bugs.php.net/bug.php?id=72392 – PapaHotelPapa Jun 12 '16 at 20:29

1 Answers1

1

It was not working for me (dont know why) so I changed the code to calculate difference in two array

for ($i = 0; $i < count($array2); $i++) { 
    for ($j = 0; $j < count($array1); $j++) {
        if(!in_array($array1[$j],$array2)){ 
            $resArr[] = $array1[$j]; 
        } 
    } 
}
print_r($array1);echo "<br>";
print_r($array2);echo "<br>";
print_r($resArr);echo "<br>";

Thanks for the time

Wazy
  • 8,822
  • 10
  • 53
  • 98