当前位置:Gxlcms > PHP教程 > 多个并列分类怎么用递归遍历出其所有子分并统计各自子分类个数

多个并列分类怎么用递归遍历出其所有子分并统计各自子分类个数

时间:2021-07-01 10:21:17 帮助过:33人阅读

从数据库的一张表中获取了一个数组,为便于分析进行了切割,切割后数组如下:

$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',
      ),
      );

现在想对该多维数组进行处理,规则为:当a键的recom_account的值等于b键的trade_account时,则a键值对为b键值对的直接子分类,当c键的recom_account等于a键的trade_account时,则c键值对为b键值对的间接子分类,每个键值对(分类)可以有多个直接子分类,但只对应一个直接父分类,也就是trade_account是唯一的,recom_account不是唯一的。期望返回的结果是:每个键值对增加一个count键,其值为所有直接子分类的个数,一个childlist键,其值为一个多维数组,记录子分类的所有信息,一个total_count键,其值为所有子分类的个数。haschild字段为1表示该分类有子分类(包括直接子分类与间接子分类),为0则没有子分类。如上面的数组,期望生成如下的格式。

$list = 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',
            '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,
        ),
    );
    

已参考http://www.thinkphp.cn/topic/...进行调试,但未成功获取返回值。

回复内容:

从数据库的一张表中获取了一个数组,为便于分析进行了切割,切割后数组如下:

$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',
      ),
      );

现在想对该多维数组进行处理,规则为:当a键的recom_account的值等于b键的trade_account时,则a键值对为b键值对的直接子分类,当c键的recom_account等于a键的trade_account时,则c键值对为b键值对的间接子分类,每个键值对(分类)可以有多个直接子分类,但只对应一个直接父分类,也就是trade_account是唯一的,recom_account不是唯一的。期望返回的结果是:每个键值对增加一个count键,其值为所有直接子分类的个数,一个childlist键,其值为一个多维数组,记录子分类的所有信息,一个total_count键,其值为所有子分类的个数。haschild字段为1表示该分类有子分类(包括直接子分类与间接子分类),为0则没有子分类。如上面的数组,期望生成如下的格式。

$list = 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',
            '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,
        ),
    );
    

已参考http://www.thinkphp.cn/topic/...进行调试,但未成功获取返回值。

人气教程排行