There are many ways to implement array search in php. I know that the simplest thing is to compare in_array with the array traversal. there are more and better methods. I will introduce them below. One-dimensional data
There are many ways to implement array search in php. I know that the simplest thing is to compare in_array with the array traversal. there are more and better methods. I will introduce them below.
One-dimensional array search is simple. in_array ()
If the value parameter is a string and the type parameter is set to true, the search area is case sensitive. the code is as follows:
-
- $ People = array ("Peter", "Joe", "Glenn", "Cleveland ");
- If (in_array ("Glenn", $ people ))
- {
- Echo "Match found ";
- }
- Else
- {
- Echo "Match not found ";
- }
- ?>
Output: Match found
Array_key_exists () function
If a specified key is found in an array, the array_key_exists () function returns true; otherwise, the function returns false in the following format:
Boolean array_key_exists (mixed key, array );
The following example searches for apple in the array key. if it is found, the color of the fruit is output. the code is as follows:
- $ Fruit ["apple"] = "red ";
- $ Fruit ["banana"] = "yellow ";
- $ Fruit ["pear"] = "green ";
- If (array_key_exists ("apple", $ fruit )){
- Printf ("apple's color is % s", $ fruit ["apple"]);
- }
- // Apple's color is red
Array_search () function
The array_search () function searches for a specified value in an array. if it is found, 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, the related information is returned. the code is as follows:
- $ 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]);
- // 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 [, 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. the code is as follows:
- $ Fruits ["apple"] = "red ";
- $ Fruits ["banana"] = "yellow ";
- $ Fruits ["watermelon"] = "green ";
- $ Keys = array_keys ($ fruits );
- Print_r ($ keys );
- // 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)
The following example gets the values of each element found in $ fruits. the code is as follows:
- $ Fruits ["apple"] = "red ";
- $ Fruits ["banana"] = "yellow ";
- $ Fruits ["watermelon"] = "green ";
- $ Values = array_values ($ fruits );
- Print_r ($ values );
- // Array ([0] => red [1] => yellow [2] => green)
All of the above is a one-dimensional array search. if you want to implement two-dimensional data or multi-dimensional data, we can refer to the following example.
Php searches for the key value of a multi-dimensional array,For example:
- $ Foo [1] ['A'] ['XX'] = 'bar 1 ';
- $ Foo [1] ['B'] ['XX'] = 'bar 2 ';
- $ Foo [2] ['A'] ['BB '] = 'bar 3 ';
- $ Foo [2] ['A'] ['yy'] = 'bar 4 ';
- $ Foo [3] ['c'] ['DD'] = 'bar 3 ';
- $ Foo [3] ['F'] ['GG '] = 'bar 3 ';
- $ Foo ['info'] [1] = 'bar 5 ';
If you want to find bar 3, how can you find it? there are three results, and all three results are required. See the following function:
- Function array_search_re ($ needle, $ haystack, $ a = 0, $ nodes_temp = array ()){
- Global $ nodes_found;
- $ A ++;
- Foreach ($ haystack as $ key1 => $ value1 ){
- $ Nodes_temp [$ a] = $ key1;
- If (is_array ($ value1 )){
- Array_search_re ($ needle, $ value1, $ a, $ nodes_temp );
- }
- Else if ($ value1 ===$ needle ){
- $ Nodes_found [] = $ nodes_temp;
- }
- }
- Return $ nodes_found;
- }
This function returns the output key name for all the content to be searched.
- $ Result = array_search_re ('Bar 3', $ foo );
- Print_r ($ result );
- // The output result is as follows:
- Array ([0] => Array ([1] => 2 [2] => a [3] => bb)
- [1] => Array ([1] => 3 [2] => c [3] => dd)
- [2] => Array ([1] => 3 [2] => f [3] => gg)
- )
Php searches for the key name of a multi-dimensional array. the code is as follows:
- Function array_search_key ($ needle, $ haystack ){
- Global $ nodes_found;
- Foreach ($ haystack as $ key1 => $ value1 ){
-
- If ($ key1 ===$ needle ){
-
- $ Nodes_found [] = $ value1;
- }
- If (is_array ($ value1 )){
- Array_search_key ($ needle, $ value1 );
- }
- }
- Return $ nodes_found;
- }
- $ Result = array_search_key ('A', $ foo );
- Print_r ($ result );
- // The output result is as follows:
- Array
- (
- [0] => Array
- (
- [Xx] => bar 1
- )
- [1] => Array
- (
- [Bb] => bar 3
- )
- [2] => Array
- (
- [Yy] => bar 4
- )
- )