Multi-dimensional array looping read related issues

Source: Internet
Author: User
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.