public function build_category_json($type, $parent_id = 0, $prefix = '')
{
if (!$category_list = $this->fetch_category($type, $parent_id))
{
return false;
}
if ($prefix)
{
$_prefix = $prefix . ' ';
}
foreach ($category_list AS $category_id => $val)
{
$data[] = array(
'id' => $category_id,
'title' => $_prefix . $val['title'],
'description' => $val['description'],
'sort' => $val['sort'],
'parent_id' => $val['parent_id'],
'url_token' => $val['url_token']
);
if ($val['child'])
{
$prefix .= '-';
$data = array_merge($data, json_decode($this->build_category_json($type, $val['id'], $prefix), true));
}
unset($prefix);
}
return json_encode($data);
}
代码中 unset($prefix); 的使用导致树结构层次上出现了问题。建议改为:
if ($val['child']) { $tmp_prefix = $prefix. '-'; // 修改 1 $data = array_merge($data, json_decode($this->build_category_json($type, $val['id'], $tmp_prefix ), true)); // 修改 2 } //unset($prefix); // 修改 3 }
AI智能回复搜索中,请稍后...