2012-10-11 4 views
0

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()를 생성하는 것입니다,하지만 난 중첩 된 개체를 만들 수있는 방법을 찾을 수 없습니다 작업.

+0

내가 대신'내가 질문했다 아니라 같은 – Baba

+0

json_encode'의이 일을하는 이유를 물어 봐도, 나는 PHP 배열을 조립하는 적절한 기능을 알아낼 수 없습니다. 이것은 내가 지금까지 얻은 가장 가까운 것입니다. 하지만 그렇습니다. Php 배열과 josn_encode()가 가장 합리적입니다. – djt

답변

1

처럼 진정 할 수 있습니다. 이 수정 된 코드가 작동합니다 :

/* 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); 
    $first = true; 

    $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 */ 
    if($first) { 
     $first = false; 
    } else { 
     /*add comma for each element after the first*/ 
     $data .= ','; 
    } 
    $data .= '{"data":"'.$name.'","attr": {"id" : "'.$value.'"},"state":"closed","children": ['.$ret.']}'; 
    } 

    /* Return the final result */ 
    return $data; 
} 
+0

첫 번째 요소에 +1 관측 가능합니다. – Baba

+0

이것이 작동하는 것 같습니다. 잘 지금까지, 고마워! – djt

1

당신은 당신이 그럼 그냥 제 이후의 요소를 추적 연결을 통해이를 구축하고 싶었 음 경우이 대신

$data = array(); 

foreach($children as $index => $value) 
{ 
    $node = array(); 
    $node['data'] = getNodeName($tree, $value) ; 
    $node['attr'] = array("id"=>$value,"state"=>"closed","children"=>walkTree($tree, $value)) ; 

    $data[] = $node ; 
} 

return json_encode($data); 
+1

이것은 내 것보다 훨씬 똑똑한 솔루션입니다. –

+0

@ Jason Sperske가 원하는 것을 달성하기 위해 개선 할 수 있다고 확신합니다. 당신은 다른 것을 필요로합니다. – Baba

+0

불행히도, 이것은 작동하지 않는 것 같습니다. '하위'키가 채워지지 않고 있으며, 각 키는 비어 있습니다. 그리고 배열에 단지 1 레벨이 있습니다. 제한된 데이터로 작업하는 – djt