Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 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 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...