Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
312 views
in Technique[技术] by (71.8m points)

Array PHP. How to get deep of element in array

$mang = array(
            '0' => array(
                'uid' => 2,
                'introducer_uid' => 1,
                'introducer_users' => array(
                                        '0' => array(
                                                'uid' => 8,
                                                'introducer_uid' => 2,
                                                'introducer_users' => array() 
                                                ),
                                        '1' => array(
                                                'uid' => 9,
                                                'introducer_uid' => 2,
                                                'introducer_users' => array()) 
                                        ) 
                ),
            '1' => array(
                'uid' => 3,
                'introducer_uid' => 1,
                'introducer_users' => array(
                                        '0' => array(
                                                'uid' => 5,
                                                'introducer_uid' => 3,
                                                'introducer_users' => array()
                                        ),
                                        '1' => array(
                                                'uid' => 6,
                                                'introducer_uid' => 3,
                                                'introducer_users' => array() 
                                        ),
                                        '2' => array(
                                                'uid' => 7,
                                                'introducer_uid' => 3,
                                                'introducer_users' => array(
                                                                        '0' => array(
                                                                                    'uid' => 10,
                                                                                    'introducer_uid' => 7,
                                                                                    'introducer_users' => array(
                                                                                                            '0' => array(
                                                                                                                'uid' => 11,
                                                                                                                'introducer_uid' => 10,
                                                                                                                'introducer_users' => array() 
                                                                                                                        ) 
                                                                                                            ) 
                                                                                    ) 
                                                                        ) 
                                                ) 
                                        ) 
                ),
            '2' => array(
                    'uid' => 4,
                    'introducer_uid' => 1,
                    'introducer_users' => array() 
                ) 
        );

My require:

Make a functions to Count deep of item in $mang array.

Sample. Deep of any item will return look like:

  • 'uid' = 10 will return deep = 3

  • 'uid' = 11 will return deep = 4

  • 'uid' = 8 will return deep = 2

  • 'uid' = 2 will return deep = 1

Sample functions look like

function count_deep($array,$uid){ return $deep; }

Anyone please help me. Thank you so much.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the following recursive function to get the depth where the uuid value is found. This version returns value 0 if the uuid value is not found at all.

function searchDepth($uid, $array, $depth = 0)
{
    $depth++;

    foreach ($array as $element)
    {
        if (isset($element['uid']) && $element['uid'] == $uid)
        {
            return $depth;
        }
        else if (isset($element['introducer_users']))
        {
            $result = searchDepth($uid, $element['introducer_users'], $depth);

            if ($result != 0)
            {
                return $result;
            }
        }
    }

    return 0;
}

And to invoke the function with the search uuid and the array

$depth = searchDepth(10, $mang);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...