包括“必答题"在随机选择中 [英] Include "required questions" in a random selection

查看:22
本文介绍了包括“必答题"在随机选择中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下方法选择一组没有任何重复的随机问题:

每个问题都有一个字段:get_field('required_question'); 有一个是/否下拉列表.始终选择是的问题必须包含在上面的循环中.

例如,该测试有 20 个问题可供选择,将随机选择 10 个问题.在 20 个问题中,有 2 个必填问题(即这些问题将始终被选中).因此,它需要抓取 2 个必填问题并选择其他 8 个随机问题.

我如何在随机选择中包含所需的问题?

解决方案

问题没有说明,但所有人都认为这是一个

请注意,这里我使用的是 $repeater_row['title'] 而不是 OP 的 $repeater_row['question'].另外,我删除了 answer_options 部分.详情见评论:

//获取字段$amount = get_field( 'select_number_of_questions' );$repeater = get_field('step_by_step_test');//通过字段名称分隔字段的辅助数组$not_enabled = array();$enabled = array();//分离foreach( $repeater 作为 $field ){if( '否' == $field['enabled'] )$not_enabled[] = $field;别的$enabled[] = $field;}//从总金额中扣除启用的$amount = (int)$amount - count( $enabled );//切片前混洗洗牌( $not_enabled );$repeater_limit = array_slice( $not_enabled, 0, $amount );//添加启用的字段并再次随机播放$final_array = array_merge( $repeater_limit, $enabled );洗牌( $final_array );foreach( $final_array 作为 $repeater_row ) {回声<p>".$repeater_row['title'] ."</p>";}

I'm selecting a set of random questions without any duplicates using the following:

<?php
$amount = get_field('select_number_of_questions');
$repeater = get_field("step_by_step_test");
shuffle($repeater);
$repeater_limit = array_slice($repeater,0,$amount);
foreach($repeater_limit as $repeater_row) {
    echo "<p>".$repeater_row['question']."</p>";
    $rows = $repeater_row['answer_options'];
    foreach($rows as $row) {
        echo $row['answer']."<br />";
    }
}
?>

Each question has a field: get_field('required_question'); that has a yes/no dropdown. The questions that have yes selected ALWAYS have to be incorporated into the loop above.

E.g The test has 20 questions to select from, 10 will be selected at random. Within the 20 questions, there are 2 required questions (i.e these will always be selected). So it will need to grab the 2 required questions and select 8 other random questions.

How can I include the required questions within the random selection?

解决方案

The Question doesn't state it, but all suggests this is an Advanced Custom Fields set up using the Repeater Add-on.

In that case, this is the test configuration I've done:

Note that here I'm using $repeater_row['title'] instead of the OP's $repeater_row['question']. Also, I removed the answer_options part. See comments for details:

// Get fields
$amount = get_field( 'select_number_of_questions' );
$repeater = get_field( 'step_by_step_test' );

// Auxiliary arrays to separate fields by Field Name
$not_enabled = array();
$enabled = array();

// Separate
foreach( $repeater as $field )
{
    if( 'no' == $field['enabled'] )
        $not_enabled[] = $field;
    else
        $enabled[] = $field;
}

// Discount the enabled from the the total amount
$amount = (int)$amount - count( $enabled );

// Shuffle before slicing
shuffle( $not_enabled );
$repeater_limit = array_slice( $not_enabled, 0, $amount );

// Add enabled fields and shuffle again
$final_array = array_merge( $repeater_limit, $enabled ); 
shuffle( $final_array );

foreach( $final_array as $repeater_row ) {
    echo "<p>" . $repeater_row['title'] . "</p>";
}

这篇关于包括“必答题"在随机选择中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆