2015-10-09 46 views
-2

我陣列字符串「病人」是:我如何檢查使用in_array在PHP

$com_arr = Array(
    [0] => patient 
    [1] => novel 
    [2] => name 
    [3] => monkey 
    [4] => yourself 
    [5] => novel 
    [6] => buttons 
    [7] => fifth 
    [8] => stories 
    [9] => English 
    [10] => sign 
    [11] => children 
) 

和可變$values_for_ex[$r-1] = patient和我的函數看起來像

if(in_array($values_for_ex[$r-1] ,$com_arr)) { 
    $replacements1 = "<span style='color:green;font-weight:700;' id='s'>" 
        . $values_for_ex[$r-1] . "</span>"; 
} 

,但條件是行不通的。爲什麼?

+0

$ r來自哪裏提供一些更多信息 – peter

+0

PHP ** in_array **搜索是**區分大小寫**請檢查兩個值是否相同 –

回答

1

這是更正後的代碼。我已將$r的值假設爲5用於測試目的。

$com_arr = array(
'patient', 
'novel', 
'name', 
'monkey', 
'yourself', 
'novel', 
'buttons', 
'fifth', 
'stories', 
'English', 
'sign', 
'children' 
); 
$r=5;// hard-coded for testing purpose 
$values_for_ex[$r-1] = "patient"; 
if(in_array($values_for_ex[$r-1] ,$com_arr)) { 
$replacements1 = "<span style='color:green;font-weight:700;' id='s'>". $values_for_ex[$r-1] . "</span>"; 
    echo $replacements1; 
}