As in the following example:
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're looking for bar 3, how do I find it? There are three results, and these three results are to look at the following function:
--------------------------------------------------------------------------------------------------------------- ----------------
code is as follows |
copy code |
function array _search_re ($needle, $haystack, $a =0, $nodes _temp=array ()) { Global $nodes _found; $a + +; foreach ($haystack as $key 1=> $value 1) { $nodes _temp[$a] = $key 1; if (Is_array ($value 1)) { array_search_re ($needle, $value 1, $a, $nodes _temp); } else if ($value 1 = = $needle) { $nodes _found[] = $nodes _temp; } } Return $nodes _found } |
--------------------------------------------------------------------------------------------------------------- ------------------
This function can return all the contents of the above search to the key name
The code is as follows |
Copy Code |
$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)
[1] => Array ([1] => 3 [2] => C [3] => DD)
[2] => Array ([1] => 3 [2] => F [3] => GG)
)
1 PHP search multidimensional array key name
code is as follows |
copy code |
function Array_search_key ($needle, $haystack) { Global $nodes _found; foreach ($haystack as $key 1=> $value 1) { if ($key 1=== $needle) { $nodes _fo und[] = $value 1; } if (Is_array ($value 1)) { Array_search_key ($needle, $value 1); } } Return $nodes _found } $result = Array_search_key (' A ', $foo); Print_r ($result); |
The output results are as follows:
The code is as follows |
Copy Code |
Array ( [0] => Array ( [XX] => bar 1 ) [1] => Array ( [BB] => bar 3 ) [2] => Array ( [yy] => Bar 4 ) ) |
Similar to full text matching! Two loops first loop keys second fuzzy match with function strstr () to solve