PHP Search Array Strings We typically use Array_search and In_array two functions
array_search() function in_array() , look 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.
But Array_search is typically used to search for the first string in an array that meets the requirements. If the searched string contains more than one array, it is not possible to use Array_search. At this point, we use the Array_keys () function, which implements the search string in the array containing multiple,
Consider the following example:
$array = Array (0 = ' Blue ', 1 = ' red ', 2 = ' green ', 3 = ' red '); $a =array_search ("Blue", $array); The output $a=0; $b =array_search (' Red ', $array); will only output $b=1; $p = Array_keys ($array, ' red ');//the string searched in the array contains multiple if (Is_array ($p)) { foreach ($p as $v) { echo $val. Appears in the. $v. " "; }} else { echo $val. " Appears in the. Array_search ($val, $array). ";}
Example two:
$array = Array (4,5,7,8,9,10); $found = Array_search (8, $array); Call the Array_search function and output the find result if ($found) {//If the output key is found echo "found, key is". $found;} else{//If the output error message is not found echo "not Found";}
Now let's take a look at the In_array function
Using In_array (Value,array,type)
Type is optional. If set to true, checks whether the searched data is the same as the type of the array's value.
$arr = Array (' Can ', ' how ', ' method ', ' Know ', ' no ', ' don't '),//in_array (value,array,type) $isin = In_array ("How 2", $arr); if ($isin) { echo "in====". $isin;} else{ echo "out====". $isin;}
The above is the whole content of this article, I hope you can enjoy
The above describes the PHP array string search Array_search techniques, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.