/* * 随机处理逻辑 * */ private function _Rand($type, $num){ if (intval($num) <= 0){ return false;} $problemArr = ProblemModel::getBaseArr([‘state‘ => 1, ‘type‘ => $type], [‘id‘], [], [‘order‘ => SORT_DESC]); $problemArr = array_column($problemArr, ‘id‘); $PaperArr = PaperProblemModel::getBaseArr([‘type‘ => $type], [‘problem_id‘]); $PaperArr = array_column($PaperArr, ‘problem_id‘); $d_count = count($problemArr); if ($d_count < $num) { $this->_error(‘数据表选题数量不够‘, []);} $Arr = array_diff($problemArr, $PaperArr); $count = count($Arr); if ($count < $num){ $return = array_rand($problemArr, $num); } else { $problemArr = array_unique($Arr); $return = array_rand($problemArr, $num); } $res = []; foreach ($return as $k => $v){ $res[$k] = $problemArr[$return[$k]]; } return $res; }
原文地址:https://www.cnblogs.com/ghjbk/p/10003293.html
时间: 2024-10-20 02:09:06