我需要递归获取类别名称。我无法将数组传递给递归函数。
function getCat($id, array &$cats) {
global $db;
$cc = $db->prepare("SELECT * FROM tbl_articlecategories WHERE id=?");
$cc->execute(array($id));
$cat = $cc->fetch(PDO::FETCH_OBJ);
$cats[] = $cat->title;
if ($cat->nodeId != 0) { // top node
getCat($cat->nodeId, $cats);
} else {
return implode(' => ', array_reverse($cats));
}
}
var_dump(getCat(21));
示例数据:
id | title | nodeId
1 Main 0
5 Electronics 1
21 TV's 5