随机抽取特定数量的问题

时间:2018-09-27 22:30:00

标签: php wordpress

我以前使用过函数,每次使用shuffle()函数进行测验时,每次以新的顺序对我进行洗牌,都会得到一组问题数组

  1. 原始数组(Q1,Q2,Q3,Q4,Q5,Q6)
  2. 第一次给我(Q2,Q4,Q1,Q3,Q6,Q5)
  3. 第二次给我(Q3,Q4,Q2,Q6,Q1,Q5),依此类推。

每次都有新问题。

这是仅适用于随机播放的功能代码

	/**
	 * Update user questions.
	 *
	 * @param $quiz_id
	 * @param $course_id
	 * @param $user_id
	 */
	public function update_user_questions( $quiz_id, $course_id, $user_id ) {
		global $wpdb;
		$item = null;

		switch ( current_action() ) {
			case 'pmi-course/user/quiz-redone':
				$item = $wpdb->get_row(
					$wpdb->prepare( "
					SELECT * FROM {$wpdb->prefix}pmicourse_user_items
					WHERE item_id = %d
						AND user_id = %d
						AND ref_id = %d
					ORDER BY user_item_id DESC
				", $quiz_id, $user_id, $course_id )
				);
				break;
			case 'pmi-course/user/quiz-started':
				break;
		}
		if ( ! $item ) {
			return;
		}
		if ( ! $item->status == 'started' ) {
			return;
		}

		$random_quiz = get_user_meta( $user_id, 'random_quiz', true );
		$quiz        = LP_Quiz::get_quiz( $quiz_id );
		if ( $quiz && $questions = $quiz->get_questions() ) {
			$questions = array_keys( $questions );
			shuffle( $questions );
			$question_id = reset( $questions );

			// set user current question
			$user = pmi_course_get_current_user();
			$user_course = $user->get_course_data( $course_id );
			$item_quiz = $user_course->get_item($quiz_id);

			$item_quiz->set_meta( '_current_question', $question_id );
			$item_quiz->update_meta();

			pmi_course_update_user_item_meta( $item->user_item_id, 'current_question', $question_id );
			if ( empty( $random_quiz ) ) {
				$random_quiz = array( $quiz_id => $questions );
			} else {
				$random_quiz[ $quiz_id ] = $questions;
			}
			update_user_meta( $user_id, 'random_quiz', $random_quiz );
		}
	}

	/**
	 * Random quiz questions.
	 *
	 * @param $quiz_questions
	 * @param $quiz_id
	 *
	 * @return array
	 */
	public function random_questions( $quiz_questions, $quiz_id ) {

		if ( get_post_meta( $quiz_id, '_lp_random_mode', true ) == 'yes' ) {

			// get user meta random quiz
			$random_quiz = get_user_meta( get_current_user_id(), 'random_quiz', true );
			if ( is_admin() || empty( $random_quiz ) || empty( $random_quiz[ $quiz_id ] ) ) {
				return $quiz_questions;
			}
			$questions = array();
			if ( array_key_exists( $quiz_id, $random_quiz ) && sizeof( $random_quiz[ $quiz_id ] ) == sizeof( $quiz_questions ) ) {
				foreach ( $random_quiz[ $quiz_id ] as $question_id ) {
					if ( $question_id ) {
						$questions[ $question_id ] = $question_id;
					}
				}
			} else {
				$question_ids = array_keys( $quiz_questions );
				shuffle( $question_ids );
				$random_quiz[ $quiz_id ] = $question_ids;
				$questions               = array();
				foreach ( $question_ids as $id ) {
					$questions[ $id ] = $quiz_questions[ $id ];
				}
			}

			return $questions;
		}

		return $quiz_questions;

	}

这是我尝试从一组中的“ 6”个问题中随机抽取特定数量的问题(例如“ 4”个问题)的方法,但是它一次给出了随机结果,而不是像shuffle()一样再次给出随机结果,我知道array_rand()每次都赋予不同的价值,但是为什么我不这样做。可能是我做错了什么。我把这两种情况都放下了,要么有人可以识别我在做什么,要么可以将解决方案扩展到更好的解决方案。 任何帮助将不胜感激。

  1. 原始数组(Q1,Q2,Q3,Q4,Q5,Q6)
  2. 第一次(Q1,Q4,Q5,Q6)
  3. 第二时间(Q1,Q4,Q5,Q6)
  4. 第三次(Q1,Q4,Q5,Q6)---我每次都希望有不同的集合

我为实现这一目标所做的努力

///for first function 

if ( $quiz && $questions = $quiz->get_questions() ) {
$questions = array_rand( $questions , 4);
$question_id = reset( $questions );


///for second function

if ( array_key_exists( $quiz_id, $random_quiz ) && sizeof( $random_quiz[ $quiz_id ] ) < sizeof( $quiz_questions ) ) {
foreach ( $random_quiz[ $quiz_id ] as $question_id ) {
if ( $question_id ) {
$questions[ $question_id ] = $question_id;
}
}
} else {
question_ids = array_rand( $quiz_questions , 4);
$random_quiz[ $quiz_id ] = $question_ids;
questions               = array();
foreach ( $question_ids as $id ) {
$questions[ $id ] = $quiz_questions[ $id ];
}
}

1 个答案:

答案 0 :(得分:0)

如果array_rand无法按照您想要的方式工作。尝试使用随机播放,然后对数组进行切片以获得具有4个元素的新数组。使用您的代码查看下面的示例

///for first function 

if ( $quiz && $questions = $quiz->get_questions() ) {
shuffle($questions);
$questions = array_slice( $questions ,0, 3);
$question_id = reset( $questions );


///for second function

if ( array_key_exists( $quiz_id, $random_quiz ) && sizeof( $random_quiz[ $quiz_id ] ) < sizeof( $quiz_questions ) ) {
foreach ( $random_quiz[ $quiz_id ] as $question_id ) {
if ( $question_id ) {
$questions[ $question_id ] = $question_id;
}
}
} else {
shuffle($quiz_questions);
question_ids = array_slice( $quiz_questions ,0, 3);
$random_quiz[ $quiz_id ] = $question_ids;
questions               = array();
foreach ( $question_ids as $id ) {
$questions[ $id ] = $quiz_questions[ $id ];
}
}