Home  >  Article  >  Backend Development  >  Detailed explanation of the steps for array_search() function to return key names by element value

Detailed explanation of the steps for array_search() function to return key names by element value

php中世界最好的语言
php中世界最好的语言Original
2018-05-21 10:10:292131browse

This time I will bring you array_search() function to return the key name by element value in detail. What are the precautions for the array_search() function to return the key name by element value? The following is a practical case, let’s take a look. The

array_search() function is the same as in_array(), which searches for a key value in an array.
If the value is found, the key name of the matching element will be returned. If not found, false is returned

array_search() definition and usage
array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, returns false.

Prior to PHP 4.2.0, functions returned null instead of false on failure.

If the third parameter strict is specified as true, the key name of the corresponding element will only be returned if the data type and value are consistent.

Syntax
array_search(value,array,strict)Parameter Description
value Required. Specifies the search for the value in the array.
array required. The array to be searched.
strict Optional. Possible values:

true
false - Default
If the value is set to true, the type of the given value will also be checked in the array. (See Example 2)

php arrayExample of function array_search:

<?php 
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse"); 
echo array_search("Dog",$a); 
?>

Output:
a

I believe I read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the steps to create scheduled tasks through the yii framework through console commands

PHP accelerator eAccelerator configuration and usage steps Detailed explanation

The above is the detailed content of Detailed explanation of the steps for array_search() function to return key names by element value. 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