2011-01-25 56 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';