PHP Recursive traversal multidimensional array implementation of infinite classification method, recursive multidimensional
In this paper, we describe the method of recursive traversal of multi-dimensional arrays in PHP for infinite classification. Share to everyone for your reference, as follows:
<?php//$data []=array (' id ' =>1, ' parentid ' =>0, ' name ' = ' China ', ' img ' = ' 52091199 '); $data []=array (' id ' =>1, ' parentid ' =>0, ' name ' = ' China '); $data []=array (' id ' =>2, ' parentid ' =>0, ' name ' = ' USA '); $data []=array (' id ' =>3, ' parentid ' =>0, ' name ' = ' Korea '); $data []=array (' id ' =>4, ' parentid ' =>1, ' name ' = ' Beijing '); $data []=array (' id ' =>5, ' parentid ' =>1, ' name ' = ' Shanghai '); $data []=array (' id ' =>6, ' parentid ' =>1, ' name ' = ' Guangxi '); $data []=array (' id ' =>7, ' parentid ' =>6, ' name ' = ' Guilin '); $data []=array (' id ' =>8, ' parentid ' =>6, ' name ' = ' Nanning '); $data []=array (' id ' =>9, ' parentid ' =>6, ' name ' = ' Liuzhou '); $data []=array (' id ' =>10, ' parentid ' =>2, ' name ' = ' New York '); $data []=array (' id ' =>11, ' parentid ' =>2, ' name ' = ' Washington '); $data []=array (' id ' =>12, ' parentid ' =>3, ' name ' = ' Seoul '); $tree =build_tree ($data, 0); Echo Memory_get_usage (); Print_r ($tree); Function Findchild (& $arr, $id) {$childs =array (); foreach ($arr as $k = = $v{if ($v [' ParentID ']== $id) {$childs []= $v; }} return $childs; } function Build_tree ($rows, $root _id) {$childs =findchild ($rows, $root _id); if (empty ($childs)) {return null; } foreach ($childs as $k = + $v) {$rescurTree =build_tree ($rows, $v [' id ']); if (null! = $rescurTree) {$childs [$k] [' Childs ']= $rescurTree; }} return $childs; }?>
Run Result:
Array ([0] = = Array ([id] = 1 [ParentID] = 0 [name] + China [Childs] + = Array ([0] = = Array ([id] = 4 [ParentID] = 1 [name] + Beijing) [1] = = Array ([ID ] = 5 [ParentID] = 1 [name] + Shanghai) [2] = = Array ([id] + 6 [paren TID] = 1 [name] + Guangxi [Childs] + = Array ([0] = = Array ([id] = > 7 [ParentID] = 6 [name] = Guilin) [1] = = Array ([id] = 8 [ParentID] = 6 [name] = Nanning) [2] = = Array ([i D] [9] [ParentID] = 6 [name] = Liuzhou)))) (1) = = Array ( [ID] + 2 [ParentID] + 0 [Name] = + US [childs] = Array ([0] = = Array ([ID] => ; Ten [ParentID] =&Gt 2 [name] + New YORK) [1] = = Array ([id] = [parentid] + 2 [name] = = WASHINGTON))) [2] = = Array ([id] = 3 [ParentID] = 0 [name] + korea [childs] = = Array ( [0] = = Array ([id] = [parentid] = 3 [name] = Seoul))))
More about PHP related content readers can view this site topic: "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", "PHP Programming Algorithm Summary", " PHP Math Skills Summary, "PHP Regular Expression Usage summary", "PHP Operations and Operator Usage Summary", "PHP string Usage Summary" and "PHP common database Operation Skills Summary"
I hope this article is helpful to you in PHP programming.
http://www.bkjia.com/PHPjc/1123795.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123795.html techarticle PHP recursive traversal multi-dimensional array implementation of the infinite classification method, recursive multidimensional This article describes the recursive traversal of PHP multi-dimensional array implementation of infinite classification method. Share to everyone for reference, with ...