-2
$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.

  • What would help is if you could tell us. How would you like to query your giant array and what would you like to return from said query? – castis Oct 13 '15 at 13:33
  • `function count_deep($array,$uid){ return $deep; }` – Phúc Phạm Hoàng Oct 13 '15 at 13:42
  • If it is school task, and the array not that deep - recursive functions with depth count will do the job with almost no code. your count_deep can call count_deep_rec($array, $uid, 0/*the initial depth*/, false/*is_found?*) Is found can be used later to stop the other recursive calls if there are and improve perf. – David Constantine Oct 13 '15 at 13:51

2 Answers2

1

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);
ejuhjav
  • 2,660
  • 2
  • 21
  • 32
0

Someone has already asked this. Best look for an answer there; however, note that in PHP it is possible to have an infinitely deep array, it would therefore be a good idea to have a maximum depth just in case things get ridiculous.

Community
  • 1
  • 1
mjsa
  • 4,221
  • 1
  • 25
  • 35