首頁 >後端開發 >php教程 >多個並列分類怎麼用遞歸遍歷出其所有子分併統計各自子分類個數

多個並列分類怎麼用遞歸遍歷出其所有子分併統計各自子分類個數

WBOY
WBOY原創
2016-08-18 09:16:341086瀏覽

從資料庫的一張表中獲取了一個數組,為便於分析進行了切割,切割後數組如下:

<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>

現在想對該多維數組進行處理,規則為:當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>

已參考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>

現在想對該多維數組進行處理,規則為:當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>

已參考http://www.thinkphp.cn/topic/...進行調試,但未成功取得回傳值。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn