我有一個員工數據數組,我正在使用該數據將其轉換爲JSON並將其傳遞給插件以創建組織結構圖。組織結構圖有多個層次,我的目標是對這些層次進行顏色編碼以顯示不同的組織結構。PHP遍歷數組並插入鍵/值
我的數組是嵌套的,有manager
和children
。
我想弄清楚如何循環這個數組併爲不同的層次分配一個顏色。
例如,數組的第一層將是藍色,下一層將是紅色等。我需要做的就是爲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
(
)
)
)
)
)
)
)
您是否有級別限制? –
@RayannNayran沒有限制。這可能是5級深度或20級深度,取決於有多少員工屬於被查看的人。 – SBB
你有沒有嘗試過實現它? –