How to implement infinite class classification via PHP loop and recursion

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.