The PHP loop and recursive implementation of infinite-level classification plays an important role in the PHP development process, and this article will explain its knowledge.
PHP enables infinite class classification by looping and recursion
First, using loops to achieve a simple level of display
Connect to a database using PDO
<!--? Phpheader ("Content-type:text/html;charset=utf-8"), $pdo = new PDO ("Mysql:host=127.0.0.1;dbname=ninthexam", "Root", "root"), $rs = $pdo---> Query ("SELECT * from City"), while ($row = $rs, Fetch ()) { $data [] = $row;}//Loop implementation Classify foreach ($data as $k + $v) { if ($v [' parint_id '] = = 0) { $tmp [] = $v; foreach ($data as $key + $value) { if ($v [' city_id '] = = $value [' parint_id ']) { $tmp [] = $value; } } }} echo ""; Print_r ($TMP);
Using recursive methods to achieve infinite pole classification display
Using recursion to achieve infinite pole classification
function Digui ($data, $parint _id = 0, $cengji = 0) { //use static to define static $tmp = Array (); foreach ($data as $k + $v) { //judgment if "level id== self-increment id" if ($v [' parint_id '] = = $parint _id) { $v [' Cengji '] = $cengji; $tmp [] = $v; Digui ($data, $v [' city_id '], $cengji + 1); } } return $tmp; } Print_r (Digui ($data, $parint _id = 0, $cengji = 0));
These are just a few ways to achieve a simple infinite class classification.
This article explains the PHP loop and recursive implementation of infinite class classification, related operations, more relevant knowledge, please focus on PHP Chinese web.
Related recommendations:
Using the Magic method __class__ in PHP to get the class name related operations
How to get the user's OpenID and basic information via PHP
PHP code to achieve more than 12,306 votes query, price query function