怎麼把資料庫查詢出的樹結構的資料迴圈賦值給數組

來源:互聯網
上載者:User
如何把資料庫查詢出的樹結構的資料迴圈賦值給數組?
//資料庫
id parentid name status
03e782cd-0f00-11e3-bcf6-001d608a0834 總公司1
49d15b3a-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834設計科1
49e24a33-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834實施科1
49f452aa-1798-11e3-991e-001d608a0834 55ac840c-0f01-11e3-bcf6-001d608a0834質檢科1
5599eec4-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834市場部1
55ac840c-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834研發部1
55ca0dad-0f01-11e3-bcf6-001d608a0834 f6597beb-0f00-11e3-bcf6-001d608a0834人事部1
b5e86db9-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834財務部1
b5f69ff5-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834經理部1
b60bc9b5-0f01-11e3-bcf6-001d608a0834 f6667833-0f00-11e3-bcf6-001d608a0834後勤部1
f6597beb-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834一公司1
f6667833-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834二公司1
f6761c03-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834三公司1
f683c3c7-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834四公司1
f6955d9f-0f00-11e3-bcf6-001d608a0834 03e782cd-0f00-11e3-bcf6-001d608a0834五公司1
//php方法代碼
public function getTreeModel() {
$sql = "select *from menutree where parentid is NULL";
$quer = $this->db->query($sql);

$row = $quer->result_array();
$data=array();
if ($quer->num_rows()>0) {
//總公司
$data=$this->getArray($row);
}
echo count($data)."
";
return $data;
}
//我這個迴圈賦值給數組有點問題。。。頁面只輸出了一二級,三四級都沒輸出,可是我var_dump($row3)都是有值的。下面這個方法望指點一下啊
public function getArray($row) {
$data=array();
foreach ($row as $val) {

$arr1['id'] = $val['id'];
$arr1['menuname'] = $val['menuname'];

$data[] = $arr1;

$sql2 = "select *from menutree where parentid ='$val[id]'";
$quer2 = $this->db->query($sql2);
$row2 = $quer2->result_array();
if ($quer2->num_rows()>0) {
foreach ($row2 as $va2) {
$arr2['id'] = $va2['id'];
$arr2['menuname'] = $va2['menuname'];

$data[] = $arr2;

//echo $va2['menuname']."
";
$sql3 = "select *from menutree where parentid ='$va2[id]'";
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.