Home > Backend Development > PHP Tutorial > PHP searches an array for a given simple instance array_search function

PHP searches an array for a given simple instance array_search function

WBOY
Release: 2016-07-28 08:29:19
Original
1026 people have browsed it

array_search()

PHP array_search() function is used to search for a given value in an array, and returns the corresponding key name if successful, otherwise it returns FALSE.

Syntax:

mixed array_search( mixed needle, array array [, bool strict] ) Parameter description:

Parameter Description
needle The value that needs to be searched in the array , if it is a string, it is case-sensitive
array The array to be retrieved
strict Optional, if set to TRUE, the value type in needle and array will also be checked

Since the starting index number of the index array may be 0, the function may also return a non-boolean value equivalent to FALSE, such as 0 or "", so you need to use the === operator to compare the values ​​returned by the function Values ​​are strictly verified.

Example:

<&#63;php
$arr_a = array(0 => "a", 1 => "b", 2 => "c");
$key = array_search("a", $arr_a);
if( $key !== FALSE ){
  echo "键名为:$key";
} else {
  echo '无匹配结果';
}
?>
Copy after login

The example output is as follows:

Key name: 0 If needle appears more than once in array, the first matching key is returned. To return the keys of all matching values, use the array_keys() function.

The above PHP search for a given simple instance in an array array_search function is all the content shared by the editor. I hope it can give you a reference, and I hope you will support this site.

The above introduces the array_search function of PHP to search a given simple instance in an array, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template