php – array_diff没有按预期工作?可能是什么原因?

我有两个数组.

检查代码

$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);

现在我得到了答案

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

但根据array_diff手册应该是

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

可能是什么问题呢

最佳答案 我按原样运行你的代码,我得到了正确的结果.

$array1 = array(0=>'215',1=> '225');
$array2 = array(0=>'225');
$result = array_diff($array1, $array2);
$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);

输出:

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

我的PHP -v

PHP 5.3.5-1ubuntu7.2 with Suhosin-Patch (cli) (built: May  2 2011 23:18:30) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

你在运行什么版本的php,

点赞