从10个随机问题中抽取04个

时间:2018-09-28 17:51:44

标签: php wordpress

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

原始数组(Q1,Q2,Q3,Q4,Q5,Q6) 第一次给我(Q2,Q4,Q1,Q3,Q6,Q5) 第二次给我(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()每次都赋予不同的价值,但是为什么我不这样做。可能是我做错了什么。我把这两种情况都放下了,要么有人可以识别我在做什么,要么可以将解决方案扩展到更好的解决方案。任何帮助将不胜感激。

原始数组(Q1,Q2,Q3,Q4,Q5,Q6) 第一次(Q1,Q4,Q5,Q6) 第二次(Q1,Q4,Q5,Q6) 第三次(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)

一个简单的函数,可以从数组中获取一组随机元素:

$questions = array("Question 1","Question 2","Question 3","Question 4","Question 5","Question 6","Question 7","Question 8","Question 9","Question 10");

function shuffle_questions($questions, $num_questions){
    $random_questions = [];

    for($i = 0; $i < $num_questions){
        $random_num = rand(0, 9);
        $random_questions[] = $questions[$random_num];
    }

    return $random_questions;
}

如果您只希望它返回独特的问题(可能是您要查找的内容)

function shuffle_questions($questions, $num_questions){
    $questions_temp = $questions;
    $random_questions = [];

    for ($i = 0; $i++; $i < $num_questions){
        $key = array_rand($questions_temp, 1);
        $random_questions[] = $questions_temp[$key];
        $unset($questions_temp[$key]);
    }

    return $random_questions;
}