Home > Article > Backend Development > PHP array search (multidimensional array search)_PHP tutorial
There are many ways to implement array search in php. I know that the simplest one is in_array and traversing the array and then comparing them one by one. There are other 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 is case sensitive
The code is as follows | Copy code | ||||||||
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 function array_key_exists() returns true, otherwise it returns false. Its form is as follows:
boolean array_key_exists(mixed key,array array);
代码如下 | 复制代码 |
//watermelon was founded on green. |
The following example will search for apple in the array key, and if found, will output the color of the fruit:
The code is as follows | Copy code |
$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 ( [0] => apple [1] => banana [2] => watermelon ) |
The code is as follows | Copy code |
$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. |
The code is as follows | Copy code |
$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 the values in an array and automatically provides a numerical index for the returned array. Its form is as follows:
array array_values(array array)
The following example will get the value of each element found in $fruits:
The code is as follows | Copy code | ||||
$fruits["apple"] = "red"; $fruits["banana"] = "yellow"; $fruits["watermelon"]="green"; $values = array_values($fruits); print_r($values); //Array ( [0] => red [1] => yellow [2] => green )
|
1 php search key value of multi-dimensional array
代码如下 | 复制代码 |
$foo[1]['a']['xx'] = 'bar 1'; 如果要查找 bar 3 怎么进行查找呢。有三个结果,而这三个结果都要,看下面的函数: print_r($result); 输出结果为如下: |
The code is as follows | Copy code |
$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 to search it? There are three results, and all three results are required. Look at 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 can return all the key names found above $result = array_search_re('bar 3', $foo); print_r($result); The output results are as follows: Array ( [0] => Array ( [1] => 2 [2] => a [3] => bb ) ) |
php搜索多维数组的键名
代码如下
|
复制代码 | ||||
foreach ($haystack as $key1=>$value1) {
if ($key1=== $needle){
$nodes_found[] = $value1;
}
if (is_array($value1)){
array_search_key($needle, $value1);
}
}