|
in_array (PHP 4, PHP 5) in_array -- 檢查陣列中是否存在某個值 說明bool in_array ( mixed needle, array haystack [, bool strict] )
在 haystack 中搜尋
needle,若果找到則返回 TRUE,否則返回 FALSE。
若果第三個參數 strict 的值為
TRUE 則 in_array() 函數還會檢查
needle 的類型是否和
haystack 中的相同。
注:
若果 needle 是字串,則比較是區分大小寫的。
注:
在 PHP 版本 4.2.0 之前,needle 不容許是一個陣列。
例子 1. in_array() 例子
<?php $os = array("Mac", "NT", "Irix", "Linux"); if (in_array("Irix", $os)) { echo "Got Irix"; } if (in_array("mac", $os)) { echo "Got mac"; } ?>
|
第二個條件失敗,因為 in_array()
是區分大小寫的,所以以上程式顯示為:
|
例子 2. in_array() 嚴格類型檢查例子
<?php $a = array('1.10', 12.4, 1.13);
if (in_array('12.4', $a, true)) { echo "'12.4' found with strict check\n"; } if (in_array(1.13, $a, true)) { echo "1.13 found with strict check\n"; } ?>
|
上例將輸出:
1.13 found with strict check
|
|
例子 3. in_array() 中用陣列作為 needle
<?php $a = array(array('p', 'h'), array('p', 'r'), 'o');
if (in_array(array('p', 'h'), $a)) { echo "'ph' was found\n"; } if (in_array(array('f', 'i'), $a)) { echo "'fi' was found\n"; } if (in_array('o', $a)) { echo "'o' was found\n"; } ?>
|
上例將輸出: 'ph' was found
'o' was found |
|
參見 array_search(),array_key_exists()
和 isset()。
| |