在递归php中以数组的形式分配每个子级的级别

时间:2015-08-28 00:03:34

标签: php

我有一个具有以下结构的数组:

Array
(
[25] => Array
    (
        [id] => 25
        [level] => 0
        [children] => Array
            (
            )

        [name] => Databases
        [parent_id] => 1
    )

[34] => Array
    (
        [id] => 34
        [level] => 0
        [children] => Array
            (
                [35] => Array
                    (
                        [id] => 35
                        [level] => 0
                        [children] => Array
                            (
                            )

                        [name] => Call Center
                        [parent_id] => 34
                    )

                [51] => Array
                    (
                        [id] => 51
                        [level] => 0
                        [children] => Array
                            (
                                [75] => Array
                                    (
                                        [id] => 75
                                        [level] => 0
                                        [children] => Array
                                            (
                                            )

                                        [name] => Accounts
                                        [parent_id] => 51
                                    )

                            )

                        [name] => Payment System
                        [parent_id] => 34
                    )

            )

        [name] => proyects
        [parent_id] => 1
    )

)

我需要在元素所在的位置分配每个子项的项级别,例如:

Array
(
[25] => Array
    (
        [id] => 25
        [level] => 0
        [children] => Array
            (
            )

        [name] => Databases
        [parent_id] => 1
    )

[34] => Array
    (
        [id] => 34
        [level] => 0
        [children] => Array
            (
                [35] => Array
                    (
                        [id] => 35
                        [level] => 1
                        [children] => Array
                            (
                            )

                        [name] => Call Center
                        [parent_id] => 34
                    )

                [51] => Array
                    (
                        [id] => 51
                        [level] => 2
                        [children] => Array
                            (
                                [75] => Array
                                    (
                                        [id] => 75
                                        [level] => 1
                                        [children] => Array
                                            (
                                            )

                                        [name] => Accounts
                                        [parent_id] => 51
                                    )

                            )

                        [name] => Payment System
                        [parent_id] => 34
                    )

            )

        [name] => proyects
        [parent_id] => 1
    )

)

我在下面的链接中使用此代码,但无法按预期工作。

http://codepad.org/TqktuMdW

<?php

$list = array(
  25 => array(
    'id' => 25,
    'name' => 'Databases',
    'parent_id' => 1,
  ),
  34 => array(
    'id' => 34,
    'name' => 'proyects',
    'parent_id' => 1,
  ),
  35 => array(
    'id' => 35,
    'name' => 'Call Center',
    'parent_id' => 34,
  ),
  51 => array(
    'id' => 51,
    'name' => 'Payment System',
    'parent_id' => 34,
  ),
  75 => array(
    'id' => 75,
    'name' => 'Accounts',
    'parent_id' => 51,
  ),    
);

$cats = array(1,2,3,4,5);

$boards = getBoardIndex($list,$cats);

print '<pre>'; print_r($boards); print '</pre>';

function getBoardIndex($data, $parent_id, $tree = array())
{
    foreach($data as $idx => $row)
    {
        if(in_array($row['parent_id'], array_values($parent_id)))
        {
            foreach($row as $k => $v)
            {
                $tree[$row['id']][$k] = $v;
                $tree[$row['id']]['level'] = in_array($row['parent_id'],array_values($parent_id)) ? 0 : array_search($k,array_keys($row));
                unset($data[$idx]);
                    $tree[$row['id']]['children'] = getBoardIndex($data,array($row['id']));
            }
        }
    }

    ksort($tree);
    return($tree);
}
?>

最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

创建一个递归函数并使用它

function assignLevels($array, $level=0) {
    foreach($array as $key => $subarray) {
        $array[$key]['children'] = assignLevels($subarray['children'], $level+1);
        $array[$key]['level'] = $level;
    }
    return $array;

}

将它传递给你的数组,它将返回指定了级别的相同数组。刚试过它