Home  >  Article  >  Backend Development  >  PHP's function array_search() searches for a key value in an array and returns the corresponding key name

PHP's function array_search() searches for a key value in an array and returns the corresponding key name

黄舟
黄舟Original
2017-11-08 13:51:407883browse

Example

Search for the key value "red" in the array and return its key name:

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

Definition and usage

array_search() function in the array Search for a key value and return the corresponding key name.

Syntax

array_search(value,array,strict)
Parameters Description
value Required. Specifies the key value to search for in the array.
array Required. Specifies the array to be searched.
strict Optional. If this parameter is set to TRUE, the function searches the array for an element with the same data type and value. Possible values:
  • true

  • false - Default

If set to true, checks the given in the array The types of values, number 5 and string 5 are different (see Example 2).

Technical details

Return value: If the specified key value is found in the array , then the corresponding key name is returned, otherwise FALSE is returned. If a key value is found more than once in the array, the key name matching the first found key value is returned.
PHP Version: 4.0.5+
Change Log: If invalid parameters are passed to the function, the function returns NULL (this applies to all PHP functions since PHP 5.3.0).

Since PHP 4.2.0, if the search fails, this function returns FALSE instead of NULL.

更多实例

实例 1

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

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

但是array_search一般用到搜索一个数组中符合要求的第一个字符串。如果搜索的字符串在数组中含有多个,使用array_search的话,是 不行的。这时,我们使用array_keys()函数,实现搜索的字符串在数组中含有多个,

看一下如下例子:

$array = array(0 => &#39;blue&#39;, 1 => &#39;red&#39;, 2 => &#39;green&#39;, 3 => &#39;red&#39;); 
$a=array_search( "blue",$array); //将输出$a=0; 
$b=array_search( &#39;red&#39;,$array); //将只会输出$b=1; 
$p = array_keys($array, &#39;red&#39;);//搜索的字符串在数组中含有多个 
if(is_array($p)) {
  foreach($p as $v) { 
    echo $val."出现在".$v . " ";
   }
}else { 
  echo $val."出现在".array_search($val, $array)." ";
}

例二:

$array = array(4,5,7,8,9,10); 
$found = array_search(8, $array); //调用array_search函数并输出查找结果 
if($found){ //如果找到输出键 
echo "已找到,键为".$found; 
}else{ //如果没有找到输出错误信息 
echo "没有找到"; 
}

下面我们来看看in_array函数

采用in_array(value,array,type)

type 可选。如果设置该参数为 true,则检查搜索的数据与数组的值的类型是否相同。

$arr = array(&#39;可以&#39;,&#39;如何&#39;,&#39;方法&#39;,&#39;知道&#39;,&#39;沒有&#39;,&#39;不要&#39;);
//in_array(value,array,type)
$isin = in_array("如何2",$arr);
if($isin){
  echo "in====".$isin;
}else{
  echo "out====".$isin;
}

The above is the detailed content of PHP's function array_search() searches for a key value in an array and returns the corresponding key name. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn