PHP數(shù)組中怎么搜索給定的值

PHP
小億
94
2024-04-02 16:43:03

在PHP中,可以使用array_search()函數(shù)來(lái)搜索給定的值在數(shù)組中的位置。示例如下:

$fruits = array("apple", "banana", "orange", "pear");
$key = array_search("orange", $fruits);

if($key !== false){
    echo "The key of 'orange' in the array is: " . $key;
} else {
    echo "'orange' is not found in the array.";
}

上述代碼將輸出:The key of ‘orange’ in the array is: 2。這表示在數(shù)組$fruits中,值為"orange"的元素在索引位置2上。

0