部門列表找每一個部門下的所有員工數,迴圈調用遞迴出現重複數組的問題

來源:互聯網
上載者:User

問題:
代碼如下:

  //尋找所有下級部門的id    public static function actionSon($company_id,$id)    {           global $temp;        $modelClass = self::find()            ->where(['parent_id'=>$id,'company_id'=>$company_id])             ->all();          if(!empty($modelClass)){            foreach ($modelClass as $k => $value) {                 $temp[] =$value->id;                self::actionSon($company_id,$value->id);//調用函數,傳入參數,繼續查詢下級            }        }        return $temp;    }    
fields裡面調用    public function fields()    {        return [            'id',            'company_id'            'user_count'=>function(){                // return $this->getUserCount();                $dep_ids=self::actionSon($this->company_id,$this->id);                  // $GLOBALS['temp']=[];               //unset($temp);print_r($dep_ids);                          },        ];    }

結果輸出:

得出重複的資料,要是列表還有部門的話會這樣一直重複下去,如何解決這個問題?
我需要的結果是每調用一次就能擷取到當前部門下的所有子部門的id,以一維數組的形式展示。
把$GLOBALS['temp']=[];放開它只能找到一級。

回複內容:

問題:
代碼如下:

  //尋找所有下級部門的id    public static function actionSon($company_id,$id)    {           global $temp;        $modelClass = self::find()            ->where(['parent_id'=>$id,'company_id'=>$company_id])             ->all();          if(!empty($modelClass)){            foreach ($modelClass as $k => $value) {                 $temp[] =$value->id;                self::actionSon($company_id,$value->id);//調用函數,傳入參數,繼續查詢下級            }        }        return $temp;    }    
fields裡面調用    public function fields()    {        return [            'id',            'company_id'            'user_count'=>function(){                // return $this->getUserCount();                $dep_ids=self::actionSon($this->company_id,$this->id);                  // $GLOBALS['temp']=[];               //unset($temp);print_r($dep_ids);                          },        ];    }

結果輸出:

得出重複的資料,要是列表還有部門的話會這樣一直重複下去,如何解決這個問題?
我需要的結果是每調用一次就能擷取到當前部門下的所有子部門的id,以一維數組的形式展示。
把$GLOBALS['temp']=[];放開它只能找到一級。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

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

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

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

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