I am trying to set up a Treeview, provided by KendoUI, based on the values stored in a two-dimensional array. Unfortunateky i can't wrap my head around the creation of the datasource, which should be a multidimensional array used in JavaScript.
The plan is to recursively read the two dimensional array and piece by piece construct the datasource in PHP as a string and then pass it to the JavaScript function.
An example of the 2-dimensional array: (the key in the 1st dimension is the 'name' of the parent element; the keys of he child elements an integer and the value is the 'name' of the child)
Array
(
[1] => Array
(
[0] => 2
[1] => 3
[2] => 4
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 3
[1] => 1
)
[4] => Array
(
[0] => 2
)
)
An example of what it should look like would be this (with two values):
dataSource: [
{ text: "1", items: [
{ text: "2", items: [
{ text: "3" }
]},
{ text: "3" , items: [
{ text: "3" },
{ text: "1" }
]},
{ text: "4", items: [
{ text: "2", items: [
{ text: "3", items: [
{ text: "3" },
{ text: "1" }
] }
] }
] }
] },
{ text: "2", items: [
{ text: "3", items: [
{ text: "3" },
{ text: "1" , items: [
{ text: "1" },
{ text: "3" },
{ text: "4", items: [
{ text: "2" }
] }
] }
] }
] }
]
I know i have to account for infinite loops in the algorithm. The direct path from the 'leaf' to the 'root' aof the tree can only contain max 2 instances of a single value. If that condition is met, the branch is abandoned and shouldn't be filled any further.
Can anybody give me some pointers in the right direction with this? Is there some PHP class i can use to simplify this problem?