您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“php無限分類樹形數(shù)據(jù)格式化代碼分享”,感興趣的朋友不妨來看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“php無限分類樹形數(shù)據(jù)格式化代碼分享”吧!
我們知道很多開源軟件的無限分類都是采用遞歸的算法,但是我們知道遞歸即浪費(fèi)時(shí)間,又浪費(fèi)空間(內(nèi)存),
上次我也分享了個(gè)我自己原創(chuàng)的無限分類生成樹的方法,一熱心php高手網(wǎng)友給我提出了寶貴的建議,我測(cè)試了一下,這段代碼的時(shí)間非常之短參考: http://www.oschina.net/code/snippet_98719_11296,我再次整理了一下,發(fā)現(xiàn)數(shù)據(jù)庫(kù)查詢出數(shù)據(jù)之后,我們就已經(jīng)定好了鍵值,因而實(shí)踐中,我們一般會(huì)在model中查詢出格式化成主鍵值對(duì)應(yīng)數(shù)據(jù)的形式,因而我們可以直接用這樣的數(shù)據(jù),就少了一層循環(huán)。代碼也非常簡(jiǎn)潔。
原來的代碼:
function genTree($items,$id='id',$pid='pid',$son = 'children'){ $tree = array(); //格式化的樹 $tmpMap = array(); //臨時(shí)扁平數(shù)據(jù) foreach ($items as $item) { $tmpMap[$item[$id]] = $item; } foreach ($items as $item) { if (isset($tmpMap[$item[$pid]])) { $tmpMap[$item[$pid]][$son][] = &$tmpMap[$item[$id]]; } else { $tree[] = &$tmpMap[$item[$id]]; } } unset($tmpMap); return $tree; } $items1 = array( array('id' => 1, 'pid' => 0, 'name' => '一級(jí)11' ), array('id' => 11, 'pid' => 0, 'name' => '一級(jí)12' ), array('id' => 2, 'pid' => 1, 'name' => '二級(jí)21' ), array('id' => 10, 'pid' => 11, 'name' => '二級(jí)22' ), array('id' => 3, 'pid' => 1, 'name' => '二級(jí)23' ), array('id' => 12, 'pid' => 11, 'name' => '二級(jí)24' ), array('id' => 9, 'pid' => 1, 'name' => '二級(jí)25' ), array('id' => 14, 'pid' => 1, 'name' => '二級(jí)26' ), array('id' => 4, 'pid' => 9, 'name' => '三級(jí)31' ), array('id' => 6, 'pid' => 9, 'name' => '三級(jí)32' ), array('id' => 7, 'pid' => 4, 'name' => '四級(jí)41' ), array('id' => 8, 'pid' => 4, 'name' => '四級(jí)42' ), array('id' => 5, 'pid' => 4, 'name' => '四級(jí)43' ), array('id' => 13, 'pid' => 4, 'name' => '四級(jí)44' ), array('id' => 15, 'pid' => 8, 'name' => '五級(jí)51' ), array('id' => 16, 'pid' => 8, 'name' => '五級(jí)52' ), array('id' => 17, 'pid' => 8, 'name' => '五級(jí)53' ), array('id' => 18, 'pid' => 16, 'name' => '六級(jí)64' ), ); var_dump(genTree($items1));
以下是補(bǔ)充:
/** * 此方法由@Tonton 提供 * http://my.oschina.net/u/918697 * @date 2012-12-12 */ function genTree5($items) { foreach ($items as $item) $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']]; return isset($items[0]['son']) ? $items[0]['son'] : array(); }
/** * 將數(shù)據(jù)格式化成樹形結(jié)構(gòu) * @author Xuefen.Tong * @param array $items * @return array */ function genTree9($items) { $tree = array(); //格式化好的樹 foreach ($items as $item) if (isset($items[$item['pid']])) $items[$item['pid']]['son'][] = &$items[$item['id']]; else $tree[] = &$items[$item['id']]; return $tree; } $items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'), 2 => array('id' => 2, 'pid' => 0, 'name' => '黑龍江省'), 3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'), 4 => array('id' => 4, 'pid' => 2, 'name' => '哈爾濱市'), 5 => array('id' => 5, 'pid' => 2, 'name' => '雞西市'), 6 => array('id' => 6, 'pid' => 4, 'name' => '香坊區(qū)'), 7 => array('id' => 7, 'pid' => 4, 'name' => '南崗區(qū)'), 8 => array('id' => 8, 'pid' => 6, 'name' => '和興路'), 9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'), 10 => array('id' => 10, 'pid' => 8, 'name' => '東北林業(yè)大學(xué)'), 11 => array('id' => 11, 'pid' => 9, 'name' => '哈爾濱工業(yè)大學(xué)'), 12 => array('id' => 12, 'pid' => 8, 'name' => '哈爾濱師范大學(xué)'), 13 => array('id' => 13, 'pid' => 1, 'name' => '贛州市'), 14 => array('id' => 14, 'pid' => 13, 'name' => '贛縣'), 15 => array('id' => 15, 'pid' => 13, 'name' => '于都縣'), 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店鎮(zhèn)'), 17 => array('id' => 17, 'pid' => 14, 'name' => '大田鄉(xiāng)'), 18 => array('id' => 18, 'pid' => 16, 'name' => '義源村'), 19 => array('id' => 19, 'pid' => 16, 'name' => '上壩村'), ); print_r(genTree5($items)); print_r(genTree9($items)); //后者輸出格式,前者類似,只是數(shù)組鍵值不一樣,不過不影響數(shù)據(jù)結(jié)構(gòu) /* Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => 江西省 [son] => Array ( [0] => Array ( [id] => 3 [pid] => 1 [name] => 南昌市 ) [1] => Array ( [id] => 13 [pid] => 1 [name] => 贛州市 [son] => Array ( [0] => Array ( [id] => 14 [pid] => 13 [name] => 贛縣 [son] => Array ( [0] => Array ( [id] => 16 [pid] => 14 [name] => 茅店鎮(zhèn) [son] => Array ( [0] => Array ( [id] => 18 [pid] => 16 [name] => 義源村 ) [1] => Array ( [id] => 19 [pid] => 16 [name] => 上壩村 ) ) ) [1] => Array ( [id] => 17 [pid] => 14 [name] => 大田鄉(xiāng) ) ) ) [1] => Array ( [id] => 15 [pid] => 13 [name] => 于都縣 ) ) ) ) ) [1] => Array ( [id] => 2 [pid] => 0 [name] => 黑龍江省 [son] => Array ( [0] => Array ( [id] => 4 [pid] => 2 [name] => 哈爾濱市 [son] => Array ( [0] => Array ( [id] => 6 [pid] => 4 [name] => 香坊區(qū) [son] => Array ( [0] => Array ( [id] => 8 [pid] => 6 [name] => 和興路 [son] => Array ( [0] => Array ( [id] => 10 [pid] => 8 [name] => 東北林業(yè)大學(xué) ) [1] => Array ( [id] => 12 [pid] => 8 [name] => 哈爾濱師范大學(xué) ) ) ) ) ) [1] => Array ( [id] => 7 [pid] => 4 [name] => 南崗區(qū) [son] => Array ( [0] => Array ( [id] => 9 [pid] => 7 [name] => 西大直街 [son] => Array ( [0] => Array ( [id] => 11 [pid] => 9 [name] => 哈爾濱工業(yè)大學(xué) ) ) ) ) ) ) ) [1] => Array ( [id] => 5 [pid] => 2 [name] => 雞西市 ) ) ) )*/
到此,相信大家對(duì)“php無限分類樹形數(shù)據(jù)格式化代碼分享”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。