2009-12-09 115 views
9

我有以下兩個數組和代碼,以查找和array_diff:PHP:不區分大小寫「和array_diff」

$obs_ws = array("you", "your", "may", "me", "my", "etc"); 
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe"); 

$final_ws = array_diff($all_ws, $obs_ws); 

以上代碼給輸出數組:

$final_ws = array("LOVE", "World", "Your", "CoDe"); 

但我想它是:

$final_ws = array("LOVE", "World", "CoDe"); 

注「您的」不被去除,它可能是由於「Y」是在第二陣列帽。我想排除「你的」,所以在PHP中有任何不區分大小寫的array_diff版本。

我試圖array_udiff,但我沒有得到究竟是如何在我的問題用這個

感謝

回答

43

試圖通過strcasecmp作爲第三個參數來array_udiff功能:

<?php 
$obs_ws = array("you", "your", "may", "me", "my", "etc"); 
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe"); 

$final_ws = array_udiff($all_ws, $obs_ws, 'strcasecmp'); 

print_r($final_ws); 

輸出:

Array 
(
    [0] => LOVE 
    [1] => World 
    [5] => CoDe 
) 
+2

我相信你錯過了一個逗號。 ;) – MitMaro 2009-12-09 18:38:11

+0

@MitMaro:謝謝! – 2009-12-09 18:40:52

+0

我試過它像'array_udiff($ all_ws,$ obs_ws,'strcasecmp');'但它不工作:( – Prashant 2009-12-09 18:45:19

3

你在正確的軌道上。這是我的建議:

function array_casecmp($arr1,$arr2){ 
    return array_udiff($arr1,$arr2,'strcasecmp'); 
} 


$obs_ws = array("you", "your", "may", "me", "my", "etc"); 
$all_ws = array("LOVE", "World", "Your", "my", "etc", "CoDe"); 
var_dump(array_casecmp($all_ws,$obs_ws)); 
+1

爲什麼不把函數名''strcasecmp''作爲第三個參數? – 2009-12-09 19:58:06

+1

你說得對,我應該。 – Jage 2009-12-09 20:26:59