PHP数组中怎么搜索给定的值

PHP
2140
2024/3/26 16:43:03
栏目: 编程语言
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

在PHP中,可以使用array_search()函数来搜索给定的值在数组中的位置。示例如下:

$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。这表示在数组$fruits中,值为"orange"的元素在索引位置2上。

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: php xmpp有哪些常见问题