数组PHP。如何深入了解数组中的元素

时间:2015-10-13 13:27:48

标签: php arrays

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

我的要求:

创建一个函数来计算$ mang数组中项目的深度。

示例 任何项目的深度将返回如下:

  • ' UID' = 10将返回深= 3

  • ' UID' = 11将返回deep = 4

  • ' UID' = 8将返回deep = 2

  • ' UID' = 2将返回deep = 1

示例函数看起来像

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

任何人都请帮助我。非常感谢你。

2 个答案:

答案 0 :(得分:1)

您可以使用以下递归函数来获取找到uuid值的深度。如果根本找不到uuid值,则此版本返回值0。

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

并使用搜索uuid和数组

调用该函数
$depth = searchDepth(10, $mang);

答案 1 :(得分:0)

有人有already asked this。最好找一个答案;但请注意,在PHP中,可能有一个无限深的数组,因此,如果事情变得荒谬,最好有一个最大深度。