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

WBOY
Lepaskan: 2016-08-18 09:16:34
asal
1068 orang telah melayarinya

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

<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>
Salin selepas log masuk
Salin selepas log masuk

现在想对该多维数组进行处理,规则为:当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(

<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>
Salin selepas log masuk
Salin selepas log masuk

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

回复内容:

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

<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>
Salin selepas log masuk
Salin selepas log masuk

现在想对该多维数组进行处理,规则为:当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(

<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>
Salin selepas log masuk
Salin selepas log masuk

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

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan