How does PHP find a key in a multidimensional array?

Source: Internet
Author: User
My array is this:

$arr = Array (
' a ' = = Array (
' 1 ' = ' [large] ',
' 2 ' = ' [small] ',
' 3 ' = ' [many] ',
' 4 ' = ' [less] ',
)
' b ' = = Array (
' 1 ' = ' [you] ',
' 2 ' = ' [i] ',
' 3 ' = ' [She] ',
' 4 ' = ' [It] ',
)
)

I want to be able to find 1 at the same time as [you] can find a second level of key B.

Array_search seemingly can only find one-dimensional array, ask everyone.

Reply content:

My array is this:

$arr = Array (
' a ' = = Array (
' 1 ' = ' [large] ',
' 2 ' = ' [small] ',
' 3 ' = ' [many] ',
' 4 ' = ' [less] ',
)
' b ' = = Array (
' 1 ' = ' [you] ',
' 2 ' = ' [i] ',
' 3 ' = ' [She] ',
' 4 ' = ' [It] ',
)
)

I want to be able to find 1 at the same time as [you] can find a second level of key B.

Array_search seemingly can only find one-dimensional array, ask everyone.

Have you ever done anything similar to infinite pole classification?? Do you know how to design a table for this function??

Example: Commodity classification table

cat_id  cat_name  pid   1       test     0  2       test1    0  3       test3    0  4       test4    1  5       test5    2  ......

This should be familiar with, if give you a classification ID, I want to find his all sub-categories including his own How to do?? Or to find all his parents, including the tower, what??

About your problem, if you have a way to format the key name of the multidimensional array into a structure similar to the above table, then you provide a value, in the array to find the value corresponding to the key, and then in the formatted array after the search, you can get the results you want.

Iterate through each of the sub-arrays, remember the name of the array is placed into the cumulative variable, the last found when the cumulative variable output.

is actually to find the leaf node in the tree.

Don't know if it's what you want.

foreach ($arr as $key=>$value) {    $ikey = array_search('[你]', $value);    if ($ikey) {       echo $key,'--',$ikey,'
'; }}
  • Related Article

    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.