亚洲精品中文字幕无乱码_久久亚洲精品无码AV大片_最新国产免费Av网址_国产精品3级片

php語言

php兩種無限分類方法實(shí)例

時(shí)間:2024-06-15 19:49:58 php語言 我要投稿
  • 相關(guān)推薦

php兩種無限分類方法實(shí)例

  本文給出了遞歸方法和非遞歸方法實(shí)現(xiàn)的無限分類實(shí)例,有需要的朋友可以參考一下,希望對大家有所幫助!想了解更多相關(guān)信息請持續(xù)關(guān)注我們應(yīng)屆畢業(yè)生考試網(wǎng)!

  一、遞歸方法

  復(fù)制代碼 代碼如下:

  $items = array(

  array('id'=>1,'pid'=>0,'name'=>'一級11'),

  array('id'=>2,'pid'=>0,'name'=>'一級12'),

  array('id'=>3,'pid'=>1,'name'=>'二級21'),

  array('id'=>4,'pid'=>3,'name'=>'三級31'),

  array('id'=>5,'pid'=>1,'name'=>'二級22'),

  array('id'=>6,'pid'=>3,'name'=>'三級32'),

  array('id'=>7,'pid'=>6,'name'=>'四級41'),

  );

  $i = 0;

  function formatTree($arr, $pid = 0){

  $tree = array();

  $temp = array();

  global $i;

  if($arr){

  foreach($arr as $k=>$v){

  if($v['pid'] == $pid){//

  $temp = formatTree($arr, $v['id']);

  $temp && $v['son'] = $temp;

  $tree[] = $v;

  }

  }

  }

  return $tree;

  }

  print_r(formatTree($items));

  二、非遞歸方法

  復(fù)制代碼 代碼如下:

  function genTree($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' => '一級11'),

  2 => array('id' => 2, 'pid' => 1, 'name' => '二級21'),

  3 => array('id' => 3, 'pid' => 1, 'name' => '二級23'),

  4 => array('id' => 4, 'pid' => 9, 'name' => '三級31'),

  5 => array('id' => 5, 'pid' => 4, 'name' => '四級43'),

  6 => array('id' => 6, 'pid' => 9, 'name' => '三級32'),

  7 => array('id' => 7, 'pid' => 4, 'name' => '四級41'),

  8 => array('id' => 8, 'pid' => 4, 'name' => '四級42'),

  9 => array('id' => 9, 'pid' => 1, 'name' => '二級25'),

  10 => array('id' => 10, 'pid' => 11, 'name' => '二級22'),

  11 => array('id' => 11, 'pid' => 0, 'name' => '一級12'),

  12 => array('id' => 12, 'pid' => 11, 'name' => '二級24'),

  13 => array('id' => 13, 'pid' => 4, 'name' => '四級44'),

  14 => array('id' => 14, 'pid' => 1, 'name' => '二級26'),

  15 => array('id' => 15, 'pid' => 8, 'name' => '五級51'),

  16 => array('id' => 16, 'pid' => 8, 'name' => '五級52'),

  17 => array('id' => 17, 'pid' => 8, 'name' => '五級53'),

  18 => array('id' => 18, 'pid' => 16, 'name' => '六級64'),

  );

  print_r(genTree($items));

【php兩種無限分類方法實(shí)例】相關(guān)文章:

php實(shí)現(xiàn)偽靜態(tài)的方法實(shí)例09-25

php畫圖實(shí)例07-16

php生成縮略圖的兩種方法05-24

PHP實(shí)用的代碼實(shí)例08-12

php查詢mysql的實(shí)例09-09

PHP socket的配置及實(shí)例10-16

php生成縮略圖的兩種方法(代碼)08-08

php簡單偽靜態(tài)實(shí)例09-16

php使用GeoIP庫實(shí)例08-09

php:樹形結(jié)構(gòu)的算法實(shí)例10-18