PHP array_search() 函数

PHP 教程 · 2019-01-22 13:26:31

实例

在数组中搜索键值 "red",并返回它的键名:

<?php
$a=array("a"=>"red","b"=>"green","c"=>"blue");
echo array_search("red",$a);
?>

定义和用法

array_search() 函数在数组中搜索某个键值,并返回对应的键名。

语法

array_search(value,array,strict)

参数 描述
value 必需。规定在数组中搜索的键值。
array 必需。规定被搜索的数组。
strict 可选。如果该参数被设置为 TRUE,则函数在数组中搜索数据类型和值都一致的元素。可能的值:
  • true
  • false - 默认
如果设置为 true,则在数组中检查给定值的类型,数字 5 和字符串 5 是不同的(参见实例 2)。

技术细节

返回值: 如果在数组中找到指定的键值,则返回对应的键名,否则返回 FALSE。如果在数组中找到键值超过一次,则返回第一次找到的键值所匹配的键名。
PHP 版本: 4.0.5+
更新日志: 如果向函数传递无效的参数,函数返回 NULL(这个适用于自 PHP 5.3.0 起的所有的 PHP 函数)。

自 PHP 4.2.0 起,如果搜索失败,该函数返回 FALSE,而不是 NULL。

更多实例

实例 1

在数组中搜索键值 5,并返回它的键名(注意 ""):

<?php
$a=array("a"=>"5","b"=>5,"c"=>"5");
echo array_search(5,$a,true);
?>

点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html

查看所有标签

The Master Switch

The Master Switch

Tim Wu / Knopf / 2010-11-2 / USD 27.95

In this age of an open Internet, it is easy to forget that every American information industry, beginning with the telephone, has eventually been taken captive by some ruthless monopoly or cartel. Wit......一起来看看 《The Master Switch》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

MD5 加密
MD5 加密

MD5 加密工具

html转js在线工具
html转js在线工具

html转js在线工具