Home > Backend Development > PHP Tutorial > How to use recursion to traverse all sub-categories of multiple parallel categories and count the number of each sub-category

How to use recursion to traverse all sub-categories of multiple parallel categories and count the number of each sub-category

WBOY
Release: 2016-08-18 09:16:34
Original
1098 people have browsed it

Acquired an array from a table in the database and cut it to facilitate analysis. The cut array is as follows:

<code>$arr = array(
  0 => 
    array(
      'uid' => 5,
      'username' => '',
      'password' => '',
      'user_status' => 1,
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '13863810574',
      'realname' => '汪精卫',
      'level' => '普通会员',
      'trade_account' => '568050269',
      'reg_time' => null,
      'recom_account' => '243556',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  1 => 
    array(
      'uid' => '6',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18807556392',
      'realname' => '周杰伦',
      'level' => '普通会员',
      'trade_account' => '12435356',
      'reg_time' => null,
      'recom_account' => '243556',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
  2 => 
    array(
      'uid' => '7',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18316850912',
      'realname' => '奥巴马',
      'level' => '普通会员',
      'trade_account' => '83475535',
      'reg_time' => null,
      'recom_account' => '834755351',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
  3 => 
    array(
      'uid' => '8',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879126845',
      'realname' => '大卫',
      'level' => '普通会员',
      'trade_account' => '834755351',
      'reg_time' => null,
      'recom_account' => '154548',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  4 => 
    array(
      'uid' => '9',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879701814',
      'realname' => '黄建',
      'level' => '普通会员',
      'trade_account' => '154548',
      'reg_time' => null,
      'recom_account' => '568050269',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  5 => 
    array(
      'uid' => '10',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879701814',
      'realname' => '张科',
      'level' => '普通会员',
      'trade_account' => '9527',
      'reg_time' => null,
      'recom_account' => '568050269',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
      );</code>
Copy after login
Copy after login

Now I want to process this multi-dimensional array. The rule is: when the value of recom_account of key a is equal to the trade_account of key b, then the key-value pair of a is a direct subcategory of the key-value pair of b. When the recom_account of key c is equal to the key of a When trade_account is used, the c key-value pair is an indirect subcategory of the b key-value pair. Each key-value pair (category) can have multiple direct subcategories, but only corresponds to one direct parent category, that is, trade_account is unique. recom_account is not unique. The expected return result is: each key-value pair adds a count key whose value is the number of all direct subcategories, a childlist key whose value is a multidimensional array that records all the information of the subcategory, and a total_count key whose value is The value is the number of all subcategories. A haschild field of 1 indicates that the category has subcategories (including direct subcategories and indirect subcategories), and a haschild field of 0 indicates that there are no subcategories. As in the above array, it is expected to generate the following format.

$list = array(

<code>        0 =>
        array(
            'uid' => 5,
            'username' => '',
            'password' => '',
            'user_status' => 1,
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '13863810574',
            'realname' => '汪精卫',
            'level' => '普通会员',
            'trade_account' => '568050269',
            'reg_time' => null,
            'recom_account' => '243556',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => '2',
            'total_count' => '4',
            'childlist' => array(
                9 => array(//9为子分类的uid
                    'uid' => '9',
                    'username' => '',
                    'password' => '',
                    'user_status' => '1',
                    'login_number' => null,
                    'last_login_time' => null,
                    'last_login_ip' => null,
                    'nickname' => '',
                    'mobile' => '18879701814',
                    'realname' => '黄建',
                    'level' => '普通会员',
                    'trade_account' => '154548',
                    'reg_time' => null,
                    'recom_account' => '568050269',
                    'total' => null,
                    'is_apply' => '0',
                    'is_checked' => '0',
                    'haschild' => '1',
                    'count' => 1,
                    'total_count' => 2,
                    'childlist' => array(
                        8 => array(
                            'uid' => '8',
                            'username' => '',
                            'password' => '',
                            'user_status' => '1',
                            'login_number' => null,
                            'last_login_time' => null,
                            'last_login_ip' => null,
                            'nickname' => '',
                            'mobile' => '18879126845',
                            'realname' => '大卫',
                            'level' => '普通会员',
                            'trade_account' => '834755351',
                            'reg_time' => null,
                            'recom_account' => '154548',
                            'total' => null,
                            'is_apply' => '0',
                            'is_checked' => '0',
                            'haschild' => '1',
                            'count' => 1,
                            'total_count'=>1,
                            'childlist' => array(
                                7 => array(//7为子分类的uid
                                    'uid' => '7',
                                    'username' => '',
                                    'password' => '',
                                    'user_status' => '1',
                                    'login_number' => null,
                                    'last_login_time' => null,
                                    'last_login_ip' => null,
                                    'nickname' => '',
                                    'mobile' => '18316850912',
                                    'realname' => '奥巴马',
                                    'level' => '普通会员',
                                    'trade_account' => '83475535',
                                    'reg_time' => null,
                                    'recom_account' => '834755351',
                                    'total' => null,
                                    'is_apply' => '0',
                                    'is_checked' => '0',
                                    'haschild' => '0',
                                    'count' => 0,
                                    'total_count' => 0,
                                    'childlist' => null,
                                )
                            )
                        )
                    )
                ),
                10 => array(
                    'uid' => '10',
                    'username' => '',
                    'password' => '',
                    'user_status' => '1',
                    'login_number' => null,
                    'last_login_time' => null,
                    'last_login_ip' => null,
                    'nickname' => '',
                    'mobile' => '18879701814',
                    'realname' => '张科',
                    'level' => '普通会员',
                    'trade_account' => '9527',
                    'reg_time' => null,
                    'recom_account' => '568050269',
                    'total' => null,
                    'is_apply' => '0',
                    'is_checked' => '0',
                    'haschild' => '0',
                    'count' => 0,
                    'total_count' => 0,
                    'childlist' => null,
                ),
            )
        ),
        1 =>
        array(
            'uid' => '6',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18807556392',
            'realname' => '周杰伦',
            'level' => '普通会员',
            'trade_account' => '12435356',
            'reg_time' => null,
            'recom_account' => '243556',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        2 =>
        array(
            'uid' => '7',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18316850912',
            'realname' => '奥巴马',
            'level' => '普通会员',
            'trade_account' => '83475535',
            'reg_time' => null,
            'recom_account' => '834755351',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        3 =>
        array(
            'uid' => '8',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879126845',
            'realname' => '大卫',
            'level' => '普通会员',
            'trade_account' => '834755351',
            'reg_time' => null,
            'recom_account' => '154548',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        4 =>
        array(
            'uid' => '9',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879701814',
            'realname' => '黄建',
            'level' => '普通会员',
            'trade_account' => '154548',
            'reg_time' => null,
            'recom_account' => '568050269',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        5 =>
        array(
            'uid' => '10',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879701814',
            'realname' => '张科',
            'level' => '普通会员',
            'trade_account' => '9527',
            'reg_time' => null,
            'recom_account' => '568050269',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
    );
    </code>
Copy after login
Copy after login

I have referred to http://www.thinkphp.cn/topic/... for debugging, but failed to obtain the return value.

Reply content:

Acquired an array from a table in the database and cut it to facilitate analysis. The cut array is as follows:

<code>$arr = array(
  0 => 
    array(
      'uid' => 5,
      'username' => '',
      'password' => '',
      'user_status' => 1,
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '13863810574',
      'realname' => '汪精卫',
      'level' => '普通会员',
      'trade_account' => '568050269',
      'reg_time' => null,
      'recom_account' => '243556',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  1 => 
    array(
      'uid' => '6',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18807556392',
      'realname' => '周杰伦',
      'level' => '普通会员',
      'trade_account' => '12435356',
      'reg_time' => null,
      'recom_account' => '243556',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
  2 => 
    array(
      'uid' => '7',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18316850912',
      'realname' => '奥巴马',
      'level' => '普通会员',
      'trade_account' => '83475535',
      'reg_time' => null,
      'recom_account' => '834755351',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
  3 => 
    array(
      'uid' => '8',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879126845',
      'realname' => '大卫',
      'level' => '普通会员',
      'trade_account' => '834755351',
      'reg_time' => null,
      'recom_account' => '154548',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  4 => 
    array(
      'uid' => '9',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879701814',
      'realname' => '黄建',
      'level' => '普通会员',
      'trade_account' => '154548',
      'reg_time' => null,
      'recom_account' => '568050269',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '1',
      ),
  5 => 
    array(
      'uid' => '10',
      'username' => '',
      'password' => '',
      'user_status' => '1',
      'login_number' => null,
      'last_login_time' => null,
      'last_login_ip' => null,
      'nickname' => '',
      'mobile' => '18879701814',
      'realname' => '张科',
      'level' => '普通会员',
      'trade_account' => '9527',
      'reg_time' => null,
      'recom_account' => '568050269',
      'total' => null,
      'is_apply' => '0',
      'is_checked' => '0',
      'haschild' => '0',
      ),
      );</code>
Copy after login
Copy after login

Now I want to process this multi-dimensional array. The rule is: when the value of recom_account of key a is equal to the trade_account of key b, then the key-value pair of a is a direct subcategory of the key-value pair of b. When the recom_account of key c is equal to the key of a When trade_account is used, the c key-value pair is an indirect subcategory of the b key-value pair. Each key-value pair (category) can have multiple direct subcategories, but only corresponds to one direct parent category, that is, trade_account is unique. recom_account is not unique. The expected return result is: each key-value pair adds a count key whose value is the number of all direct subcategories, a childlist key whose value is a multidimensional array that records all the information of the subcategory, and a total_count key whose value is The value is the number of all subcategories. A haschild field of 1 indicates that the category has subcategories (including direct subcategories and indirect subcategories), and a haschild field of 0 indicates that there are no subcategories. As in the above array, it is expected to generate the following format.

$list = array(

<code>        0 =>
        array(
            'uid' => 5,
            'username' => '',
            'password' => '',
            'user_status' => 1,
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '13863810574',
            'realname' => '汪精卫',
            'level' => '普通会员',
            'trade_account' => '568050269',
            'reg_time' => null,
            'recom_account' => '243556',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => '2',
            'total_count' => '4',
            'childlist' => array(
                9 => array(//9为子分类的uid
                    'uid' => '9',
                    'username' => '',
                    'password' => '',
                    'user_status' => '1',
                    'login_number' => null,
                    'last_login_time' => null,
                    'last_login_ip' => null,
                    'nickname' => '',
                    'mobile' => '18879701814',
                    'realname' => '黄建',
                    'level' => '普通会员',
                    'trade_account' => '154548',
                    'reg_time' => null,
                    'recom_account' => '568050269',
                    'total' => null,
                    'is_apply' => '0',
                    'is_checked' => '0',
                    'haschild' => '1',
                    'count' => 1,
                    'total_count' => 2,
                    'childlist' => array(
                        8 => array(
                            'uid' => '8',
                            'username' => '',
                            'password' => '',
                            'user_status' => '1',
                            'login_number' => null,
                            'last_login_time' => null,
                            'last_login_ip' => null,
                            'nickname' => '',
                            'mobile' => '18879126845',
                            'realname' => '大卫',
                            'level' => '普通会员',
                            'trade_account' => '834755351',
                            'reg_time' => null,
                            'recom_account' => '154548',
                            'total' => null,
                            'is_apply' => '0',
                            'is_checked' => '0',
                            'haschild' => '1',
                            'count' => 1,
                            'total_count'=>1,
                            'childlist' => array(
                                7 => array(//7为子分类的uid
                                    'uid' => '7',
                                    'username' => '',
                                    'password' => '',
                                    'user_status' => '1',
                                    'login_number' => null,
                                    'last_login_time' => null,
                                    'last_login_ip' => null,
                                    'nickname' => '',
                                    'mobile' => '18316850912',
                                    'realname' => '奥巴马',
                                    'level' => '普通会员',
                                    'trade_account' => '83475535',
                                    'reg_time' => null,
                                    'recom_account' => '834755351',
                                    'total' => null,
                                    'is_apply' => '0',
                                    'is_checked' => '0',
                                    'haschild' => '0',
                                    'count' => 0,
                                    'total_count' => 0,
                                    'childlist' => null,
                                )
                            )
                        )
                    )
                ),
                10 => array(
                    'uid' => '10',
                    'username' => '',
                    'password' => '',
                    'user_status' => '1',
                    'login_number' => null,
                    'last_login_time' => null,
                    'last_login_ip' => null,
                    'nickname' => '',
                    'mobile' => '18879701814',
                    'realname' => '张科',
                    'level' => '普通会员',
                    'trade_account' => '9527',
                    'reg_time' => null,
                    'recom_account' => '568050269',
                    'total' => null,
                    'is_apply' => '0',
                    'is_checked' => '0',
                    'haschild' => '0',
                    'count' => 0,
                    'total_count' => 0,
                    'childlist' => null,
                ),
            )
        ),
        1 =>
        array(
            'uid' => '6',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18807556392',
            'realname' => '周杰伦',
            'level' => '普通会员',
            'trade_account' => '12435356',
            'reg_time' => null,
            'recom_account' => '243556',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        2 =>
        array(
            'uid' => '7',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18316850912',
            'realname' => '奥巴马',
            'level' => '普通会员',
            'trade_account' => '83475535',
            'reg_time' => null,
            'recom_account' => '834755351',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        3 =>
        array(
            'uid' => '8',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879126845',
            'realname' => '大卫',
            'level' => '普通会员',
            'trade_account' => '834755351',
            'reg_time' => null,
            'recom_account' => '154548',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        4 =>
        array(
            'uid' => '9',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879701814',
            'realname' => '黄建',
            'level' => '普通会员',
            'trade_account' => '154548',
            'reg_time' => null,
            'recom_account' => '568050269',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '1',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
        5 =>
        array(
            'uid' => '10',
            'username' => '',
            'password' => '',
            'user_status' => '1',
            'login_number' => null,
            'last_login_time' => null,
            'last_login_ip' => null,
            'nickname' => '',
            'mobile' => '18879701814',
            'realname' => '张科',
            'level' => '普通会员',
            'trade_account' => '9527',
            'reg_time' => null,
            'recom_account' => '568050269',
            'total' => null,
            'is_apply' => '0',
            'is_checked' => '0',
            'haschild' => '0',
            'count' => 0,
            'total_count' => 0,
            'childlist' => null,
        ),
    );
    </code>
Copy after login
Copy after login

I have referred to http://www.thinkphp.cn/topic/... for debugging, but failed to obtain the return value.

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template