Questions about multi-dimensional array loop reading

Source: Internet
Author: User
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.

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.