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.

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

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题