Rumah php教程 PHP源码 php无限分类单表实现思路

php无限分类单表实现思路

May 25, 2016 pm 05:12 PM

           

 1, "pid" => 0, "name" => "蛋糕"),
      array("id" => 2, "pid" => 0, "name" => "饼干"),
      array("id" => 3, "pid" => 0, "name" => "点心"),
      array("id" => 4, "pid" => 0, "name" => "茶饮"),
      array("id" => 5, "pid" => 0, "name" => "其它"),
 
      array("id" => 6, "pid" => 1, "name" => "甜蛋糕"),
      array("id" => 7, "pid" => 2, "name" => "原味饼干"),
      array("id" => 8, "pid" => 3, "name" => "甜点心"),
      array("id" => 9, "pid" => 5, "name" => "其它美食"),
      array("id" => 10, "pid" => 4, "name" => "养颜冷饮"),
 
      array("id" => 11, "pid" => 6, "name" => "甜心小米蛋糕"),
      array("id" => 12, "pid" => 7, "name" => "原味动物饼干"),
      array("id" => 13, "pid" => 9, "name" => "其它精美小食品"),
      array("id" => 14, "pid" => 8, "name" => "甜味德国小点心"),
      array("id" => 15, "pid" => 10, "name" => "超冷啊啊啊"),
 
      array("id" => 16, "pid" => 1, "name" => "提拉米苏是蛋糕吗"),
      array("id" => 17, "pid" => 1, "name" => "也许提拉米苏不是"),
      array("id" => 18, "pid" => 8, "name" => "德国小点心子类"),
      array("id" => 19, "pid" => 8, "name" => "德国小点心子类2"),
 
  );


$runTime = 0;

function T($data, $keys=array(0), $tmp = array()) {
	global $runTime;
     if(!empty($data)) {
         foreach($data as $k => $d) {
             $pid = $d['pid'];
             $id = $d['id'];
			 $name = $d['name'];
             if(in_array($pid, $keys))   {
				$t = array("name" => $name);
				T($data, array($id), &$t);
                $tmp["id:$id"] = $t;
                unset($data[$k]);
             }
         }
     }
	$runTime++;
 }

$tmp = array();
 $t = T($data, array(0), &$tmp);
print_r($tmp);

$num = count($data);
echo "\n------总纪录数为:< $num >  复杂度为:< $runTime >  ------\n";
Salin selepas log masuk

                                   

Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn

Alat AI Hot

Undresser.AI Undress

Undresser.AI Undress

Apl berkuasa AI untuk mencipta foto bogel yang realistik

AI Clothes Remover

AI Clothes Remover

Alat AI dalam talian untuk mengeluarkan pakaian daripada foto.

Undress AI Tool

Undress AI Tool

Gambar buka pakaian secara percuma

Clothoff.io

Clothoff.io

Penyingkiran pakaian AI

AI Hentai Generator

AI Hentai Generator

Menjana ai hentai secara percuma.

Alat panas

Notepad++7.3.1

Notepad++7.3.1

Editor kod yang mudah digunakan dan percuma

SublimeText3 versi Cina

SublimeText3 versi Cina

Versi Cina, sangat mudah digunakan

Hantar Studio 13.0.1

Hantar Studio 13.0.1

Persekitaran pembangunan bersepadu PHP yang berkuasa

Dreamweaver CS6

Dreamweaver CS6

Alat pembangunan web visual

SublimeText3 versi Mac

SublimeText3 versi Mac

Perisian penyuntingan kod peringkat Tuhan (SublimeText3)