Home > Article > Backend Development > Detailed explanation of the steps for array_search() function to return key names by element value
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:
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!