溫馨提示×

array_search在關聯(lián)數(shù)組中的應用技巧

PHP
小樊
83
2024-08-16 17:53:34
欄目: 編程語言

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’的鍵。最后,我們輸出了找到的鍵。

0