What is the use of array_search () in php?
❮ PHP Array Reference Show ExampleSearch an array for the value "red" and return its key: $a=array("a"=>"red","b"=>"green","c"=>"blue"); Try it Yourself » Definition and UsageThe array_search() function search an array for a value and returns the key. Syntaxarray_search(value, array, strict) Parameter Values
Technical Details
More ExamplesExampleSearch an array for the value 5 and return its key (notice the ""): $a=array("a"=>"5","b"=>5,"c"=>"5"); Try it Yourself » ❮ PHP Array Reference (PHP 4 >= 4.0.5, PHP 5, PHP 7, PHP 8) array_search — Searches the array for a given value and returns the first corresponding key if successful Descriptionarray_search(mixed Parametersneedle The searched value.
haystack The array. strict If the third parameter Return Values Returns the key for If Warning This function may return Boolean ExamplesExample #1 array_search() example
See Also
turabgarip at gmail dot com ¶ 5 years ago
opencart dot ocfilter at gmail dot com ¶ 11 months ago
(array_search('needle', [ 0 => 0 ])); // int(0) (!)var_dump(array_search('needle', [ 0 => 0 ], true)); // bool(false)?> thinbegin at gmail dot com ¶ 4 years ago
cue at openxbox dot com ¶ 19 years ago
while (($key = array_search("a", $code)) != NULL)
stefano@takys dot it ¶ 11 years ago
nordseebaer at gmx dot de ¶ 2 years ago
yasien dot dwieb at gmail dot com ¶ 2 years ago
maciej at speccode dot com ¶ 7 years ago
= [ RichGC ¶ 16 years ago
$i = array_search("apple", $fruit_array)) n-regen ¶ 13 years ago
codeslinger at compsalot dot com ¶ 12 years ago
[$index] = array('key'=>$key, 'data'=>'foo');
stooshie at gmail dot com ¶ 10 years ago
helenadeus at gmail dot com ¶ 13 years ago
= array('a','b','a','b');$needle = 'a';print_r(array_search_all($needle, $haystack));//Output will be azaozz, gmail ¶ 13 years ago
.array_search("str1", $a, true).", andreas dot damm at maxmachine dot de ¶ 14 years ago
kermes [at] thesevens [dot] net ¶ 14 years ago
$result; What is the difference between In_array and array_search?The main difference between both the functions is that array_search() usually returns either key or index whereas in_array() returns TRUE or FALSE according to match found in search. Value: It specifies the value that needs to be searched in an array.
How do you get a specific value from an array in PHP?The array_search() is an inbuilt function in PHP that is used to search for a particular value in an array, and if the value is found then it returns its corresponding key. If there are more than one values then the key of the first matching value will be returned.
What is the use of Is_array () function?The is_array() function checks whether a variable is an array or not. This function returns true (1) if the variable is an array, otherwise it returns false/nothing.
How do you check if a key exists in an array PHP?PHP array_key_exists() Function
The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.
|