Baobab 데이터 (트리 구조)의 JSON 객체를 어셈블하려고합니다. 나는 다음과 같은 재귀 함수가 있습니다다차원 JSON 객체 조립
/* Takes the Tree (in this case, always '1') and the Parent ID where we want to start, this case, I would also start with '1' */
function walkTree($tree, $id)
{
/* Gets the children of that of that ID */
$children = $tree->getChildren($id);
$data = "";
/* Loop through the Children */
foreach($children as $index => $value)
{
/* A function to get the 'Name' associated with that ID */
$name = getNodeName($tree, $value);
/* Call the walkTree() function again, this time based on that Child ID */
$ret = walkTree($tree, $value);
/* Append the string to $data */
$data .= '{"data":"'.$name.'","attr": {"id" : "'.$value.'"}
,"state":"closed","children": ['.$ret.']}';
}
/* Return the final result */
return $data;
}
이 작업에 매우 가까이하지만 당신이 보는대로,이 중첩 된 각 객체와 배열 사이에 쉼표가 없다, 그래서 JSON 형식이 올바르지 않습니다. 다음의 제비 : 나는 가장 좋은 방법을 생각
... {"data":"Personal","attr": {"id" : "4"},"state":"closed","children": []}{"data":"News-editorial","attr": {"id" : "5"},"state":"closed","children": []
는 ... PHP는 배열과 json_encode()
를 생성하는 것입니다,하지만 난 중첩 된 개체를 만들 수있는 방법을 찾을 수 없습니다 작업.
내가 대신'내가 질문했다 아니라 같은 – Baba
json_encode'의이 일을하는 이유를 물어 봐도, 나는 PHP 배열을 조립하는 적절한 기능을 알아낼 수 없습니다. 이것은 내가 지금까지 얻은 가장 가까운 것입니다. 하지만 그렇습니다. Php 배열과 josn_encode()가 가장 합리적입니다. – djt