tags:

views:

191

answers:

1
function createJsonTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {

 foreach ($array as $categoryId => $category) {

  if ($currentParent == $category['parent']) {      

   if ($currLevel > $prevLevel) $output .= ' , "children":[ '; 

   if ($currLevel == $prevLevel) $output .= " }, ";

    $output .= '{ "data" :'.'"'.$category['menu_title'].'"';

   if ($currLevel > $prevLevel) { $prevLevel = $currLevel; }

   $currLevel++; 

    $output .= self::createJsonTree($array, $category['id'], $currLevel, $prevLevel);

    $currLevel--;      
  } 

 }

 if ($currLevel == $prevLevel) $output .= " }] ";
 return $output;
}
A: 

Is there a reason that json_encode won't work for this?

http://us3.php.net/json_encode

Jeff