2011-01-25 73 views
0

好吧,假设我有2个数组。比较2个数组并修改1

$ myArray =('1','2','3','4','5','6','7','8','9','10') ;
$ badNumbers =( '3', '6', '10')

我想要做的就是$badNumbers比较$myArrays,然后修改$myArrays来去除$badNumbers发现任何东西。

因此,一些码之后,最后的结果将是:

$ myArray的=( '1', '2', '4', '5',7' , '8',“9 “);
$ badNumbers =( '3', '6', '10')

反正有没有做到这一点?我有一些东西,但似乎没有任何工作。单独的比较部分我已经有一些问题。

编辑:我很好,也有第三个数组。对于每个值,如果它没有出现在第二个数组中,则为array_push。但我仍然不太确定如何做到这一点。

回答

1

您可以使用array_diff来获得结果。

$myArray= array('1','2','3','4','5','6','7','8','9','0'); 
$badNumbers= array('3','6','0'); 
$available = array_diff($myArray, $badNumbers); 

print_r($available); 

echo '<br /><br />' . implode(', ', $available); 

希望这会有所帮助。

+0

哇。我怎么会错过PHP手册中的这个页面,我永远不知道。谢谢。 – 2011-01-25 04:30:00

1
$result = array_diff($myArray, $badNumbers); 
echo count($result) ? 'there were differences' : 'there werent'; 
0

“count”是做什么的? “回声”就像“印刷”?

echo count($result) ? 'there were differences' : 'there werent';