我在嵌套集合模型https://en.wikipedia.org/wiki/Nested_set_model中有一个数组,我需要将数组转换为具有无限级别的json父级子节点。
数组示例:
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"}]
有人可以帮我创建一个函数来进行转换吗?