PHP 无限循环

PHP 无限循环

December 6th, 2019 阅读 527 字数 45 评论 0 喜欢 627
    public function random_infinite_cycle(){
        $arr =[
            1=>[
                23=>23, 24=>24, 25=>25, 26=>26,
            ],
            2=>[
                13=>13, 14=>14, 15=>15, 16=>16, 33=>33, 35=>35,
            ],
            3=>[
                53=>53, 54=>54, 55=>55, 56=>56, 67=>67, 13=>13, 3=>3, 53=>53,
            ],
        ];
        //总条数
        $send_mount = 15;
        $result_arr =[];
        $this->cycle_arr($arr, $send_mount, $result_arr);
        $arr_result = $res_plan_ids =[];
        if(!empty($result_arr)){
            foreach ($result_arr as $v_arr){
                foreach ($v_arr as $v_i){
                    $res_plan_ids[] = $v_i;
                }
            }
            $num = 1;
            for ($i =0; $i< $send_mount; $i++){
                if(!empty($res_plan_ids[$i])){
                    $arr_result[] = $res_plan_ids[$i];
                    $num++;
                }
            }
        }
        print_r($arr_result);
    }



    public function cycle_arr($arr, $send_mount, &$result_arr){
        //每个数组只随机三条
        $amount = 3;
        $arr_surplus = [];
        foreach ($arr as $v){
            $arr_count = count($v);
            //当前数据小于等于3,全部拿出来
            if($arr_count <= $amount){
                $rs_detail = array_values($v);
            } else { //默认随机3条
                $rs_key = array_rand($v,$amount);
                $rs_detail =[];
                foreach($rs_key as $v_key){
                    $rs_detail[] = $v[$v_key];
                }
            }
            $result_arr[] = $rs_detail;
            //去除已被拿出的数据
            $res_rand =[];
            foreach ($v as $k_r => $v_r){
                if(!in_array($v_r,$rs_detail)){
                    if(!empty($v_r)){
                        $res_rand[] = $v_r;
                    }
                }
            }
            //未使用的数据
            if(!empty($res_rand)){
                $arr_surplus[] = $res_rand;
            }
        }
        $count_recursive = count($result_arr,COUNT_RECURSIVE) - count($result_arr);
        $difference = $send_mount - $count_recursive;
        if($difference >= 0){
            $this->cycle_arr($arr_surplus,$difference + $count_recursive,$result_arr);
        }
    }

评论列表

发表评论

电子邮件地址不会被公开。 必填项已用 *标注