Finding, filtering, and searching array elements are some of the common features of array manipulation. Here are a few related functions.
In_array () function
The In_array () function searches for a specific value in an array rollup and returns True if this value is found, otherwise false. The form is as follows:
1 |
boolean in_array(mixed needle,arrayhaystack[,boolean strict]); |
Look at the following example, look for the variable if Apple is already in the array, and if so, output a piece of information:
2 |
$fruits= array("apple","banana","orange","pear"); |
3 |
if( in_array($fruit,$fruits) ) |
The third parameter is optional, which forces In_array () to consider the type when searching.
Array_key_exists () function
If a specified key is found in an array, the function array_key_exists () returns True, otherwise false is returned. The form is as follows:
1 |
boolean array_key_exists(mixed key,arrayarray); |
The following example searches for Apple in the array key and, if found, outputs the color of the fruit:
1 |
$fruit["apple"] = "red"; |
2 |
$fruit["banana"] = "yellow"; |
3 |
$fruit["pear"] = "green"; |
4 |
if(array_key_exists("apple", $fruit)){ |
5 |
printf("apple‘s color is %s",$fruit["apple"]); |
The result of executing this code:
Array_search () function
The Array_search () function searches for a specified value in an array, returns the corresponding key if found, otherwise returns false. The form is as follows:
1 |
mixed array_search(mixed needle,arrayhaystack[,boolean strict]) |
The following example searches for a specific date in $fruits (December 7) and, if found, returns information about the corresponding state:
1 |
$fruits["apple"] = "red"; |
2 |
$fruits["banana"] = "yellow"; |
3 |
$fruits["watermelon"]="green"; |
4 |
$founded= array_search("green", $fruits); |
6 |
printf("%s was founded on %s.",$founded, $fruits[$founded]) |
The results of the program run as follows:
1 |
watermelon was founded on green. |
Array_keys () function
The Array_keys () function returns an array that contains all the keys found in the searched array. The form is as follows:
1 |
arrayarray_keys(arrayarray[,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:
1 |
$fruits["apple"] = "red"; |
2 |
$fruits["banana"] = "yellow"; |
3 |
$fruits["watermelon"]="green"; |
4 |
$keys= array_keys($fruits); |
The results of the program run as follows:
1 |
Array ( [0] => apple [1] => banana [2] => watermelon ) |
Array_values () function
The Array_values () function returns all the values in an array and automatically provides a numeric index for the returned array. The form is as follows:
1 |
arrayarray_values(arrayarray) |
The following example gets the values of each element found in $fruits:
1 |
$fruits["apple"] = "red"; |
2 |
$fruits["banana"] = "yellow"; |
3 |
$fruits["watermelon"]="green"; |
4 |
$values= array_values($fruits); |
The results of the program run as follows:
1 |
Array ( [0] => red [1] => yellow [2] => green ) |
Original link: http://www.nowamagic.net/librarys/posts/php/44
PHP Array method