Multidimensional array Looping read problem
Multidimensional arrays
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 do I read the corresponding array based on this string?
Like what
A.A2 = [' A '] [' A2 '] result is an array (' DD ', ' FF ', ' GG ')
B.B2 = [' B '] [' B2 '] result is an array (' 22 ', ' 55 ', ' 88 ')
B.b3.cs = [' B '] [' B3 '] [' CS '] result is an array (' NN ', ' mm ', ' xx ')
Note: Array depth is not limited, it is best not to traverse, never use eval
By the way, does Eval have a poor efficiency in addition to safety issues?
------Solution--------------------
PHP Code
$ar = Array (' A ' =>array (' A2 ' =>array (' dd ', ' FF ', ' GG '), ' A3 ' =>array (' dd ', ' FF ', ' GG '), ' B ' =>array (' B2 ' =&G T;array (' x ', ' B3 ', ' n '), ' =>array ' (' CS ' =>array (' nn ', ' mm ', ' xx '))); $s = ' b.b3.cs '; $p =& $ar; foreach (expl Ode ('. ', $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 (' a ') ', ' ' in ', ' "') ', ' B3 ' =>array ( ' CS ' =>array (' nn ', ' mm ', ' xx ')); $s = ' b.b3.cs '; $s = explode ('. ', $s); $p =& $ar; for ($i =0; $i
------solution--------------------
Since it is absolutely impossible to use Eval
and not to traverse
then how do you intend to identify the key values that are separated by". "?
------Solution--------------------
If you want to use no foreach, you can write a recursive value for your own function if you don't consider efficiency, but the recursive function is inefficient in PHP