Home>Article>Backend Development> What are the ways to find a value in an array in php
Search method: 1. Use the "in_array($search,$arr,$type)" statement; 2. Use the "array_search($search,$arr,$type)" statement; 3. Use "array_key_exists" ($search,$arr)" statement.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
php search array Method of a certain value
Method 1: Use the in_array() function
The in_array() function searches whether the specified value exists in the array.
Method 2: array_search() function
array_search() function is the same as in_array(), searching for an item in the array key value. If the value is found, the key of the matching element is returned. If not found, returns false.
"Dog","b"=>"Cat","c"=>5,"d"=>"5"); echo array_search("Dog",$a); echo array_search("5",$a); ?>
Method 3: Use the array_key_exists() function
array_key_exists() function to check whether the specified key exists in an array name, returns true if the key name exists, false if the key name does not exist.
"Dog","b"=>"Cat"); if(array_key_exists("a",$a)){ echo "Key exists!"; }else{ echo "Key does not exist!"; } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What are the ways to find a value in an array in php. For more information, please follow other related articles on the PHP Chinese website!