Multi-dimensional array loop read problem multi-dimensional array (& nbsp; 'A' = & gt; array (& nbsp; 'A2 '= & gt; array ('DD ', 'ff', 'GG '), & nbsp; 'A3' = & gt; array ('DD', 'FF ', 'GG') & nbsp ;), & nbsp multi-dimensional array circular reading
Multi-dimensional array
Array (
'A' => array (
'A2 '=> array ('DD', 'ff', 'GG '),
'A3 '=> array ('DD', 'ff', 'GG ')
),
'B' => array (
'B2' => array ('22', '55', '88 '),
'B3' => array (
'CS '=> array ('nn', 'mm', 'XX ')
)
)
)
String
A. a2
B. b2
B. b3.cs
How can I read the corresponding array based on this string?
For example
A. a2 = ['A'] ['A2 '] The result is array ('DD', 'ff', 'GG ')
B. b2 = ['B'] ['B2'] The result is array ('22', '55', '88 ')
B. b3.cs = ['B'] ['b3'] ['CS '] The result is array ('nn', 'mm', 'XX ')
Note: The array depth is not limited. it is best not to traverse the array. eval is definitely not used.
By the way, in addition to security issues, is eval inefficient?
------ Solution --------------------
PHP code
$ Ar = array ('a' => array ('A2 '=> array ('DD', 'ff', 'GG '), 'A3 '=> array ('DD', 'ff', 'GG'), 'B' => array ('B2' => array ('22 ', '55', '88'), 'b3' => array ('CS '=> array ('nn', 'mm', 'XX ')))); $ s = 'B. b3.cs '; $ p = & $ ar; foreach (explode ('. ', $ s) as $ k) {if (isset ($ p [$ k]) $ p = $ p [$ k];} print_r ($ p );
------ Solution --------------------
PHP code
$ Ar = array ('a' => array ('A2 '=> array ('DD', 'ff', 'GG '), 'A3 '=> array ('DD', 'ff', 'GG'), 'B' => array ('B2' => array ('22 ', '55', '88'), 'b3' => array ('CS '=> array ('nn', 'mm', 'XX ')))); $ s = 'B. b3.cs '; $ s = explode ('. ', $ s); $ p = & $ ar; For ($ I = 0; $ I
------ Solution --------------------
Since eval cannot be used
Do you want to traverse
So how are you going to identify these key values separated?
------ Solution --------------------
If you want to avoid foreach, you can write a function recursive value without considering the efficiency, but the efficiency of recursive functions in php is terrible.