Searching, filtering, and searching array elements are common functions of Array Operations. The following describes several related functions.
In_array () function
The in_array () function searches for a specific value in an array. If this value is found, true is returned. Otherwise, false is returned. The format is as follows:
boolean in_array(mixed needle,array haystack[,boolean strict]);
Let's take a look at the example below to check whether the variable apple is already in the array. If so, a piece of information is output:
$ Fruit = "apple"; $ fruits = array ("apple", "banana", "orange", "Pear"); If (in_array ($ fruit, $ fruits )) echo "$ fruit is already in the array ";
The third parameter is optional. It forces in_array () to consider the type during search.
Array_key_exists () function
If a specified key is found in an array, the array_key_exists () function returns true; otherwise, false. The format is as follows:
boolean array_key_exists(mixed key,array array);
The following example searches for Apple in the array key. If it is found, the color of the fruit is output:
$fruit["apple"] = "red";$fruit["banana"] = "yellow";$fruit["pear"] = "green";if(array_key_exists("apple", $fruit)){ printf("apple's color is %s",$fruit["apple"]);}
The result of executing this code is as follows:
apple's color is red
Array_search () function
The array_search () function searches for a specified value in an array. If yes, the corresponding key is returned. Otherwise, false is returned. The format is as follows:
mixed array_search(mixed needle,array haystack[,boolean strict])
The following example searches for a specific date (December 7) in $ fruits. If it is found, information about the corresponding state is returned:
$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$founded = array_search("green", $fruits);if($founded) printf("%s was founded on %s.",$founded, $fruits[$founded])
The program running result is as follows:
watermelon was founded on green.
Array_keys () function
The array_keys () function returns an array containing all the keys found in the searched array. The format is as follows:
array array_keys(array array[,mixed search_value])
If the optional parameter search_value is included, only the key that matches the value is returned. The following example outputs all the arrays found in the $ fruit array:
$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$keys = array_keys($fruits);print_r($keys);
The program running result is as follows:
Array ( [0] => apple [1] => banana [2] => watermelon )
Array_values () function
The array_values () function returns all values in an array and automatically provides a numerical index for the returned array. The format is as follows:
array array_values(array array)
The following example gets the values of each element found in $ fruits:
$fruits["apple"] = "red";$fruits["banana"] = "yellow";$fruits["watermelon"]="green";$values = array_values($fruits);print_r($values);
The program running result is as follows:
Array ( [0] => red [1] => yellow [2] => green )
Note: This article transfers from http://www.nowamagic.net/librarys/posts/php/44