我选择一组不使用任何如下重复随机问题:
<?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 />";
}
}
?>
每个问题具有字段: get_field('required_question');
有一个是/否的下拉列表。 已是始终处于选中状态的问题必须纳入环以上。
例如测试有20个问题,以从中选择时,10将随机地选择。 在20个问题中,有2个所需的问题(即这些将永远被选中)。 因此,它需要抢2点所需的问题,并选择8点其他随机的问题。
我怎样才能包括随机选择中所需要的吗?
这个问题没有说明,但一切都表明这是一种先进的自定义字段设置成使用中继器加载项 。
在这种情况下,这是测试配置我做:
请注意,这里我使用$repeater_row['title']
而不是OP的$repeater_row['question']
另外,我删除了answer_options
部分。 详情请参阅评论:
// 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>";
}
首先,你需要过滤掉,像这样要求的问题:
$all_questions = get_field("step_by_step_test");
$required = $optional = array();
foreach($all_questions as $question) {
if( $a['required_question']) $required[] = $question;
else $optional[] = $question;
}
$amount = get_field("select_number_of_questions")-count($required);
shuffle($optional);
$final = array_merge($required,array_slice($optional,0,$amount));
foreach($final as $repeater_row) {
...
}
希望我再帮你:P