PHP array_search () function Use _php tutorial

Source: Internet
Author: User

Definition and usage

The Array_search () function, like In_array (), looks for a key value in the array. If the value is found, the key name of the matching element is returned. Returns False if it is not found.

Before PHP 4.2.0, the function returned null instead of false on failure.

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

Grammar

Array_search (value,array,strict)
Parameters Description
Value Necessary. Specifies the value to search for in the array.
Array Necessary. 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 is also checked in the array. (see Example 2)


Example #1 Array_search () example
Copy CodeThe code is as follows:
$array = Array (0 = ' Blue ', 1 = ' red ', 2 = ' green ', 3 = ' red ');

$key = Array_search (' green ', $array); $key = 2;
$key = Array_search (' red ', $array); $key = 1;
?>


Warning
This function may return a Boolean value of false, but it may also return a non-Boolean value equivalent to False, such as 0 or "". See the Boolean Types section for more information. You should use the = = = operator to test the return value of this function.


Example 1
Copy CodeThe code is as follows:
$a =array ("a" = "Dog", "b" = "Cat", "C" and "Horse");
Echo Array_search ("Dog", $a);
?>

Output: A

Example 2
Copy CodeThe code is as follows:
$a =array ("a" = "5", "B" =>5, "c" = "5");
Echo Array_search (5, $a, true);
?>

Output:
B

http://www.bkjia.com/PHPjc/321549.html www.bkjia.com true http://www.bkjia.com/PHPjc/321549.html techarticle define and use the Array_search () function, like In_array (), to find a key value in the array. If the value is found, the key name of the matching element is returned. If not found, return ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.