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

php无限分类单表实现思路

May 25, 2016 pm 05:12 PM

           

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

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";

Copy after login

                                   

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)