php遞歸實(shí)現(xiàn)無(wú)限分類的方法
本文實(shí)例講述了php遞歸實(shí)現(xiàn)無(wú)限分類的方法。分享給大家供大家參考。具體如下:
<?php $rows = array( array( 'id' => 1, 'name' => 'dev', 'parentid' => 0 ), array( 'id' => 2, 'name' => 'php', 'parentid' => 1 ), array( 'id' => 3, 'name' => 'smarty', 'parentid' => 2 ), array( 'id' => 4, 'name' => 'life', 'parentid' => 0 ), array( 'id' => 5, 'name' => 'pdo', 'parentid' => 2 ), array( 'id' => 6, 'name' => 'pdo-mysql', 'parentid' => 5 ), array( 'id' => 7, 'name' => 'java', 'parentid' => 1 ) ); // 72648 // 84072 function findChild(&$arr,$id){ $childs=array(); foreach ($arr as $k => $v){ if($v['parentid']== $id){ $childs[]=$v; } } return $childs; } function build_tree($root_id){ global $rows; $childs=findChild($rows,$root_id); if(empty($childs)){ return null; } foreach ($childs as $k => $v){ $rescurTree=build_tree($v[id]); if( null != $rescurTree){ $childs[$k]['childs']=$rescurTree; } } return $childs; } $tree=build_tree(0); echo memory_get_usage(); print_r($tree); ?>
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP實(shí)現(xiàn)繪制二叉樹圖形顯示功能詳解【包括二叉搜索樹、平衡樹及紅黑樹】
這篇文章主要介紹了PHP實(shí)現(xiàn)繪制二叉樹圖形顯示功能,結(jié)合實(shí)例形式分析了php繪制常見二叉樹的相關(guān)操作技巧,包括二叉搜索樹、平衡樹及紅黑樹的實(shí)現(xiàn)方法,需要的朋友可以參考下2017-11-11Lumen timezone 時(shí)區(qū)設(shè)置方法(慢了8個(gè)小時(shí))
今天用 Lumen 框架寫代碼時(shí), 也是初次體驗(yàn) Lumen, 遇到了一個(gè)問(wèn)題, 從數(shù)據(jù)庫(kù)里查出的時(shí)間比數(shù)據(jù)庫(kù)里保存的 TIMESTAMP 時(shí)間慢了8個(gè)小時(shí), 很明顯這是一個(gè)時(shí)區(qū)設(shè)置的問(wèn)題, 本以為可以在1分鐘內(nèi)解決的, 但是我錯(cuò)了2018-01-01PHP實(shí)現(xiàn)關(guān)鍵字搜索后描紅功能示例
這篇文章主要介紹了PHP實(shí)現(xiàn)關(guān)鍵字搜索后描紅功能,結(jié)合實(shí)例形式分析了php數(shù)據(jù)庫(kù)連接、查詢、字符串轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2019-07-07詳解如何用PHP?實(shí)現(xiàn)多進(jìn)程
這篇文章主要為大家介紹了如何用PHP?實(shí)現(xiàn)多進(jìn)程實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09PHP swoole中http_server的配置與使用方法實(shí)例分析
這篇文章主要介紹了PHP swoole中http_server的配置與使用方法,結(jié)合實(shí)例形式分析了swoole中swoole_http_server類的功能及http_server的配置、創(chuàng)建服務(wù)相關(guān)使用技巧,需要的朋友可以參考下2020-03-03PHP的serialize序列化數(shù)據(jù)以及JSON格式化數(shù)據(jù)分析
這篇文章的內(nèi)容是PHP的serialize序列化數(shù)據(jù)以及JSON格式化數(shù)據(jù)分析,需要的朋友可以參考下2015-10-10