PHP和MYSQL的演算法疑問

來源:互聯網
上載者:User
已知表t1

    id     txt          parent    --------------------------    1      上山打老虎    0    2      一二三四五    0    3      老虎不在家    1    4      啦啦啦啦啦    1    5      啊啊啊啊啊    2    6      嗯嗯恩恩恩    0    .    .    .

希望通過一次查詢

mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")

經過php對資料的重新排列

這裡的代碼應該怎麼寫???

得到如下結構的資料


  
  • 嗯嗯恩恩恩
  • 一二三四五
    • 啊啊啊啊啊
  • 上山打老虎
    • 啦啦啦啦啦
    • 老虎不在家

這個過程中的PHP處理代碼或者演算法應該怎麼寫?求指點。

回複內容:

已知表t1

    id     txt          parent    --------------------------    1      上山打老虎    0    2      一二三四五    0    3      老虎不在家    1    4      啦啦啦啦啦    1    5      啊啊啊啊啊    2    6      嗯嗯恩恩恩    0    .    .    .

希望通過一次查詢

mysql_query("SELECT id,txt,parent FROM t1 ORDER BY id DESC")

經過php對資料的重新排列

這裡的代碼應該怎麼寫???

得到如下結構的資料


  
  • 嗯嗯恩恩恩
  • 一二三四五
    • 啊啊啊啊啊
  • 上山打老虎
    • 啦啦啦啦啦
    • 老虎不在家

這個過程中的PHP處理代碼或者演算法應該怎麼寫?求指點。

 Array        (            [1] => Web Server            [3] => Nginx            [4] => Apache        )    [2] => Array        (            [2] => Database            [5] => MySQL        )    [6] => Array        (            [6] => Browser        ))

Code

/** * 無限子級遞迴迴圈 * @param  [type]  $data [description] * @param  integer $pid  [description] * @return [type]        [description] */function unlimitedForChild ($data, $pid = 0) {    $array = array();    foreach ($data as $value) {        if ($value['parent'] == $pid) {            $value['child'] = unlimitedForChild($data, $value['id']);            $array[] = $value;        }    }    return $array;}$data = array(    array(        'id' => 1,        'txt' => '上山打老虎',        'parent' => 0    ),    array(        'id' => 2,        'txt' => '一二三四五',        'parent' => 0    ),    array(        'id' => 3,        'txt' => '老虎不在家',        'parent' => 1    ),    array(        'id' => 4,        'txt' => '啦啦啦啦啦',        'parent' => 1    ),    array(        'id' => 5,        'txt' => '啊啊啊啊啊',        'parent' => 2    ),    array(        'id' => 6,        'txt' => '嗯嗯恩恩恩',        'parent' => 0    ),    //附加3級    array(        'id' => 7,        'txt' => '第三級',        'parent' => 3    ),);$result = unlimitedForChild($data);echo '
';print_r($result);echo '
';

Result

Array(    [0] => Array        (            [id] => 1            [txt] => 上山打老虎            [parent] => 0            [child] => Array                (                    [0] => Array                        (                            [id] => 3                            [txt] => 老虎不在家                            [parent] => 1                            [child] => Array                                (                                    [0] => Array                                        (                                            [id] => 7                                            [txt] => 第三級                                            [parent] => 3                                            [child] => Array                                                (                                                )                                        )                                )                        )                    [1] => Array                        (                            [id] => 4                            [txt] => 啦啦啦啦啦                            [parent] => 1                            [child] => Array                                (                                )                        )                )        )    [1] => Array        (            [id] => 2            [txt] => 一二三四五            [parent] => 0            [child] => Array                (                    [0] => Array                        (                            [id] => 5                            [txt] => 啊啊啊啊啊                            [parent] => 2                            [child] => Array                                (                                )                        )                )        )    [2] => Array        (            [id] => 6            [txt] => 嗯嗯恩恩恩            [parent] => 0            [child] => Array                (                )        ))

 Array        (            [name] => Web Server            [sub] => Array                (                    [3] => Array                        (                            [name] => Nginx                        )                    [4] => Array                        (                            [name] => Apache                        )                )        )    [2] => Array        (            [name] => Database            [sub] => Array                (                    [5] => Array                        (                            [name] => MySQL                        )                )        )    [6] => Array        (            [name] => Browser        ))
  • 相關文章

    聯繫我們

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