0

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

                                        )


                                )

                        )


                )

        )

)
6
  • Do you have a level limit? Commented May 18, 2017 at 14:38
  • @RayannNayran There are no limits in place. This could be 5 levels deep or 20 levels deep depending on how many employees fall under the person being viewed. Commented May 18, 2017 at 14:38
  • 2
    Have you tried anything to implement it? Commented May 18, 2017 at 14:39
  • I was able to traverse the arrary with a normal loop but I wasnt able to distinguish the level it was on. I had access to the seed in the loop but they were all different which gave me a different class for every record, not putting them into groups like i intended Commented May 18, 2017 at 14:41
  • Ok, put that in your question, so we can see that and help you better! Commented May 18, 2017 at 14:50

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This worked perfectly for what I needed. Thanks a lot.
Well, I'm glad it's been helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.