PHP array_search()
Published Sep 8, 2023
Contribute to Docs
The array_search() function searches an array for a given value and returns the first matching key for that value.
Syntax
array_search(value, array, strict)
value: The value to search for.array: Thearrayto search in.strict: Optional argument that defaults tofalseif not given. When set totrue, the function will search for identical elements to the value. For example, a strict search for an integer will not match a string version of that integer.
Note: String values are case-sensitive for this function.
Example
In this example, the h key is returned using the array_search() method.
<?php$a = array("a"=>"andrew","b"=>"byrne","h"=>"hozier");echo array_search("hozier",$a);?>
The output looks like this:
h
Codebyte Example
The following codebyte example can be run and uses the array_search() method.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PHP on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours