PHP迭代数组并插入键/值

时间:2017-05-18 14:35:26

标签: php arrays json

我正在使用一系列员工数据,我将其转换为JSON并将其传递给插件以创建组织结构图。组织结构图有多个级别,我的目标是对这些级别进行颜色编码以显示不同的组织。

我的数组已嵌套,并且包含managerchildren

我试图找出如何循环遍历此数组并为不同级别指定颜色。

例如,数组的第一级是蓝色,下一级是红色等等。我需要做的是为class添加一个键,然后添加它的值为levelx(其中x是深度的水平数)。

这里的最终目标是能够弄清楚如何在同一级别的所有记录上添加相同的键/值。

以下是播放class key的数组示例。

是否有任何PHP函数可以在嵌套数组中确定其级别,这将使这更容易?

Array
(
    [0] => Array
        (
            [QID] => Q1234
            [MgrQID] => Array
                (
                )

            [NTID] => xxxxx
            [MgrNTID] => xxxx
            [title] => xxxx
            [MgrName] => xxxx
            [name] => Bob Jones
            [class] => level1
            [CountOfDirects] => 9
            [children] => Array
                (
                    [0] => Array
                        (
                            [QID] => Q56789
                            [MgrQID] => 1234
                            [NTID] => xxxx
                            [MgrNTID] => xxxx
                            [title] => xxxx
                            [MgrName] => xxxx
                            [name] => Tim Cook
                            [class] => level2
                            [CountOfDirects] => 0
                            [children] => Array
                                (
                                )

                        )

                    [1] => Array
                        (
                            [QID] => Q5678
                            [MgrQID] => Q1234
                            [NTID] => xxxxx
                            [MgrNTID] => xxxx
                            [title] => xxxx
                            [MgrName] => xxxx
                            [name] => Bob Tom
                            [class] => level2
                            [CountOfDirects] => 0
                            [children] => Array
                                (
                                )

                        )


                    [2] => Array
                        (
                            [QID] => Q9999
                            [MgrQID] => Q1234
                            [NTID] => xxxx
                            [MgrNTID] => xxxx
                            [title] => xxxx
                            [MgrName] => xxxx
                            [name] => xxxx
                            [class] => level2
                            [CountOfDirects] => 0
                            [children] => Array
                                (
                                )

                        )

                    [3] => Array
                        (
                            [QID] => Q6665
                            [MgrQID] => Q1234
                            [NTID] => xxxx
                            [MgrNTID] => xxxx
                            [title] => xxxx
                            [MgrName] => xxxx
                            [name] => xxxx
                            [class] => level2
                            [CountOfDirects] => 6
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [QID] => Q4322
                                            [MgrQID] => Q6665
                                            [NTID] => xxxx
                                            [MgrNTID] => xxxx
                                            [title] => xxxx
                                            [MgrName] => xxxx
                                            [name] => xxxx
                                            [class] => level3
                                            [CountOfDirects] => 0
                                            [children] => Array
                                                (
                                                )

                                        )

                                    [1] => Array
                                        (
                                            [QID] => Q3333
                                            [MgrQID] => Q6665
                                            [NTID] => xxxx
                                            [MgrNTID] => xxxx
                                            [title] => xxxx
                                            [MgrName] => xxxx
                                            [name] => xxxx
                                            [class] => level3
                                            [CountOfDirects] => 0
                                            [children] => Array
                                                (
                                                )

                                        )


                                )

                        )


                )

        )

)

1 个答案:

答案 0 :(得分:1)

我会创建一个递归遍历数组的函数:

function setLevel(&$arr, $level)
{
    foreach ($arr as &$element) {
        $element['class'] = 'level-' . $level;
        setLevel($element['children'], $level + 1);
    }
}

setLevel($arr, 1);

请参阅demo