![](/img/trans.png)
[英]In a PHP recursive function, how can I check it's the last iteration?
[英]How can I return a variable at last call of recursive function?
我有一個遞歸函數。 如何在上次調用時從此遞歸函數返回值?
public function deneme($parent_id = 0, $sub_mark = 0, $str = '') {
$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id = ' . $parent_id);
$this->db->order_by('id', 'ASC');
$query = $this->db->get();
if($query->num_rows() > 0) {
$str .= '<ul>';
foreach($query->result_array() as $row) {
if ($parent_id == 0) {
$str .= '<li class="active">';
} else {
$str .= '<li>';
}
$str .= '<a href="index.html">' . $row['name'] . '</a> </li> ';
$sub_mark++;
$this->deneme($row['id'], $sub_mark, $str);
}
$str .= '</ul>';
}
}
我想返回str
變量。
似乎您需要進行一些更改,首先要從例程的最后返回構建的字符串。
}
$str .= '</ul>';
}
return $str;
}
第二個是遞歸調用例程的地方,您需要將返回值設置為所生成的字符串。
$str = $this->deneme($row['id'], $sub_mark, $str);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.