array_search函數(shù)用于在數(shù)組中搜索給定的值,并返回對應的鍵。在關聯(lián)數(shù)組中,可以使用array_search函數(shù)來查找特定值對應的鍵。
以下是一個示例:
$fruits = array(
'apple' => 'red',
'banana' => 'yellow',
'orange' => 'orange',
'grape' => 'purple'
);
$key = array_search('orange', $fruits);
echo "The key for 'orange' is: " . $key; // 輸出:The key for 'orange' is: orange
在上面的示例中,我們創(chuàng)建了一個關聯(lián)數(shù)組$fruits,然后使用array_search函數(shù)查找值為’orange’的鍵。最后,我們輸出了找到的鍵。