我有一個嵌套集模型數組https://en.wikipedia.org/wiki/Nested_set_model,我需要將數組轉換爲具有無限級別的json父 - 孩子。轉換嵌套集數組使用PHP
陣列例如:
array(6) {
[0]=>
array(8) {
["title"]=>
string(5) "cat 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "128"
["lft"]=>
string(1) "1"
["rgt"]=>
string(1) "4"
["depth"]=>
string(1) "0"
}
[1]=>
array(8) {
["title"]=>
string(5) "sub 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "129"
["lft"]=>
string(1) "2"
["rgt"]=>
string(1) "3"
["depth"]=>
string(1) "1"
}
[2]=>
array(8) {
["title"]=>
string(5) "cat 1"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "130"
["lft"]=>
string(1) "5"
["rgt"]=>
string(2) "10"
["depth"]=>
string(1) "0"
}
[3]=>
array(8) {
["title"]=>
string(5) "sub 2"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value2"
["id"]=>
string(3) "131"
["lft"]=>
string(1) "6"
["rgt"]=>
string(1) "9"
["depth"]=>
string(1) "1"
}
[4]=>
array(8) {
["title"]=>
string(5) "sub 3"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value3"
["id"]=>
string(3) "132"
["lft"]=>
string(1) "7"
["rgt"]=>
string(1) "8"
["depth"]=>
string(1) "2"
}
[5]=>
array(8) {
["title"]=>
string(4) "item"
["type"]=>
string(3) "url"
["class_name"]=>
string(0) ""
["content"]=>
string(6) "value1"
["id"]=>
string(3) "133"
["lft"]=>
string(2) "11"
["rgt"]=>
string(2) "12"
["depth"]=>
string(1) "0"
}
}
JSON例如:
[{"name":"Cat 1","value":"value1","children":[{"name":"sub 2", "value":"value2","children":[{"name":"sub 3", "value":"value 3"}]}]}, {"name":"Cat 2","value":"value 2","children":[{"name":"sub 1", "value":"value1"}]},{"name":"item","value":"value1"}]
有人可以幫我創建一個函數來完成轉換?
答案你想要的代碼? –
我看到深度,但不明白親子關係。在哪裏? – splash58