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;
}
问问题
1155 次
2 回答
2
json_encode 对此不起作用是否有原因?
于 2010-03-08T18:59:34.703 回答
1
您可以生成这样的树:
function createTree()
{
$sql = "SELECT id, parent_id, title FROM category order by id ";
$filas = UtilConexion::$pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
$arbol = '{"data":[{"attr":{"id":"000"},"data":"Nodo raíz","state":"open"' .
$this->createJsonTree($filas, null) . '}]}';
return $arbol;
}
于 2011-12-09T04:28:12.670 回答