chciałbym z drzewa zapisanego rekurencyjnie w formacie JSON stworzyć strukturę płaską.
Próbkę pliku JSON załączam, a oczekiwany format miałby wyglądać tak (fragment):
id;parent_id
36;35
74;35
35;1
1:0
55;54
57;54
54;1
76:0
77:0
Oczywiście json_decode i ładowanie z pliku potrafię wykonać. Tylko co dalej...
JSON:
Kod
[
{
"id":1,
"children":[
{
"id":35,
"children":[
{
"id":36,
"children":[
]
},
{
"id":74,
"children":[
]
}
]
},
{
"id":54,
"children":[
{
"id":55,
"children":[
]
},
{
"id":57,
"children":[
]
}
]
}
]
},
{
"id":76,
"children":[
]
},
{
"id":77,
"children":[
]
}
]
{
"id":1,
"children":[
{
"id":35,
"children":[
{
"id":36,
"children":[
]
},
{
"id":74,
"children":[
]
}
]
},
{
"id":54,
"children":[
{
"id":55,
"children":[
]
},
{
"id":57,
"children":[
]
}
]
}
]
},
{
"id":76,
"children":[
]
},
{
"id":77,
"children":[
]
}
]
Temat udało się rozwiązać następującym kodem:
$jsonArray = json_decode($jsonString, true); function parseJsonArray($jsonArray, $parentID = 0) { foreach ($jsonArray as $subArray) { $returnSubSubArray = parseJsonArray($subArray['children'], $subArray['id']); } } return $return; }