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

   2024-10-25 8250
核心提示:在PHP中,可以使用array_search()函数来搜索给定的值在数组中的位置。示例如下:$fruits = array(apple, banana, orange, pear);

在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上。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号