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