1、array_search()
//判斷鍵值是否在數組中,
//存在,返回值對應的鍵;
//不存在,返回false;
//例子:
$type = array(
"選考" => 'optional',
"必考" => 'necessary',
"其他" => 'other',
);
echo $subject_type = array_search('optional',$type);
//輸出:選考
2、in_array()
//和第一個類似,但是返回值不一樣。
//如果type為true,則判斷類型;type不寫,則不判斷類型;
//搜索存在,返回:true; 反之,返回:false。
$type = array(
"選考" => 'optional',
"必考" => 'necessary',
"其他" => 'other',
);
$res = in_array('optional',$type);
var_dump($res);//true
3、array_key_exists()
//該函數檢查某個數組中是否存在指定的鍵名,
//如果鍵名存在則返回 true,如果鍵名不存在則返回 false。
$search = array(
'first' => 1,
'second' => 4
);
if (array_key_exists('first', $search)) {
echo "The 'first' element is in the array";
}else{
echo "The 'first' element is not in the array";
}
//The 'first' element is in the array
----------------------------------------------------------
------------------------------------------------------------
array_key_exists() 與 isset() 的對比
isset() 對於數組中為 NULL 的值不會返回 TRUE,而 array_key_exists() 會。
$search_array = array('first' => null, 'second' => 4);
// returns false
isset($search_array['first']);
// returns true
array_key_exists('first', $search_array);
