求好的方法~?

來源:互聯網
上載者:User
資料如:
INSERT INTO `vm_nav` (`id`, `ctype`, `cid`, `name`, `ifshow`, `vieworder`, `opennew`, `url`, `type`) VALUES(1, NULL, 0, 'index', 0, 1, 0, '', 'nav'),(2, NULL, 0, 'product', 1, 2, 0, '', 'nav'),(3, NULL, 0, 'member', 1, 3, 0, '', 'nav'),(4, NULL, 0, 'guide', 1, 4, 0, '', 'nav'),(5, NULL, 0, 'kehu', 1, 5, 0, '', 'nav'),(6, NULL, 0, 'about', 1, 6, 0, '', 'nav'),(7, NULL, 0, 'tehui', 1, 7, 0, '', 'nav'),(8, NULL, 3, 'member_index', 1, 1, 0, '', 'nav'),(9, NULL, 3, 'member_info', 1, 2, 0, '', 'nav');


求這種資料
array(8) {  [0]=>  array(6) {    ["id"]=>    string(1) "2"    ["name"]=>    string(12) "product"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }  [1]=>  array(6) {    ["id"]=>    string(1) "3"    ["name"]=>    string(18) "member"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }  [2]=>  array(6) {    ["id"]=>    string(1) "8"    ["name"]=>    string(12) "member_index"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "3"  }  [3]=>  array(6) {    ["id"]=>    string(1) "9"    ["name"]=>    string(12) "member_info"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "3"  }  [4]=>  array(6) {    ["id"]=>    string(1) "4"    ["name"]=>    string(12) "guide"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }  [5]=>  array(6) {    ["id"]=>    string(1) "5"    ["name"]=>    string(12) "kehu"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }  [6]=>  array(6) {    ["id"]=>    string(1) "6"    ["name"]=>    string(12) "about"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }  [7]=>  array(6) {    ["id"]=>    string(1) "7"    ["name"]=>    string(12) "tehui"    ["opennew"]=>    string(1) "0"    ["url"]=>    string(0) ""    ["ctype"]=>    NULL    ["cid"]=>    string(1) "0"  }}


我用了一個笨辦法:先foreach吧cid 插入到一個son成員裡 然後再一個foreach 放到其後面,描述的不清楚,代碼如下:

function nav_list(){    $navlist = get_navigator();    sort($navlist['nav']);   //$navlist['nav']  就是取出所有資料 ,上面用sort排了下序    foreach($navlist['nav'] as $k=>$v)    {        if($v['cid'] != 0)        {            //插入到一個son成員裡            $l[$v['cid']]['son'][] = $v;                    }else{        $l[$v['id']] = $v;        }    }    foreach($l as $k=>$v){        if($v['son']){            //消除son成員            $tmp[] = array_diff_key($v,array('son'=>''));            foreach($v['son'] as $key=>$val){                //再一個foreach 放到其後面                $tmp[] = $val;            }        }else{            $tmp[] = $v;        }    }    return $tmp;}


~ 怎麼樣會快捷一點 ~??? 求解


回複討論(解決方案)

if(cid == id)
這條cid資料就放在id資料後面 說不清楚了~~~~

select id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid=0unionselect cid as id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid>0order by id

一位元組轉化二維?

select id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid=0unionselect cid as id, ctype, cid, name, ifshow, vieworder, opennew, url, type from vm_nav where ifshow and cid>0order by id


這樣取出來的資料 cid在父id前面:如
INSERT INTO `vm_nav` (`id`, `ctype`, `cid`, `name`, `ifshow`, `vieworder`, `opennew`, `url`, `type`) VALUES(2, NULL, 0, 'product', 1, 2, 0, '', 'nav'),(3, NULL, 3, 'member_info', 1, 2, 0, '', 'nav'),(3, NULL, 3, 'member_index', 1, 1, 0, '', 'nav'),(3, NULL, 0, 'member', 1, 3, 0, '', 'nav'),(4, NULL, 0, 'guide', 1, 4, 0, '', 'nav'),(5, NULL, 0, 'kehu', 1, 5, 0, '', 'nav'),(6, NULL, 0, 'about', 1, 6, 0, '', 'nav'),(7, NULL, 0, 'tehui', 1, 7, 0, '', 'nav');

好像是我吧事情搞複雜了~ ~

一位元組轉化二維?


嗯 ,但 一維數組中如果有cid則需要排在cid後面,cid可以理解為父節點的下標,根節點的cid=0~~~ 大概就這個意思 ~

不應該的,排序鍵相同時是按自然順序的

可帶上 cid 排序
order by id, cid

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.