dsxpt62448 2017-05-18 14:35
浏览 62
已采纳

PHP迭代数组并插入键/值

I have an array of employee data that I am working with that I convert to JSON and pass it to a plugin to create an org chart. The org chart has multiple levels and my goal is to color code those levels to show the different orgs.

My array is nested and has the manager and the children.

I am trying to figure out how I can loop over this array and assign a color for the different levels.

For example, the very first level of the array would be Blue, the next level would be red etc. All I need to do is add in a key for class and then its value which would be levelx (where x is the number of levels deep it is).

The end goal here is just to be able to figure out how to add the same key/value on all of the records on the same level.

Here is an example of the array with the class key in play.

Are there any PHP functions that can determine its level within a nested array that will make this easier?

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条回答 默认 最新

  • douqulv6059 2017-05-18 16:23
    关注

    I would create a function to walk array recursively:

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

    See demo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?