将额外的循环纳入随机选择?

I have the code below that is selecting a random set of questions from Wordpress.

<?php
    $rows = get_field('step_by_step_test');
    $row_count = count($rows);
    $rand_rows = array();
    $questions = get_field('select_number_of_questions');
    for ($i = 0; $i < min($row_count, $questions); $i++) {
        $r = rand(0, $row_count - 1);
        while (array_search($r, $rand_rows) !== false) {
            $r = rand(0, $row_count - 1);
        }
        $rand_rows[] = $r;
        echo $rows[$r]['question'];
    }
?>

I want to incorporate a bit of extra code (below), how can I make sure it's selecting the same random question?

<?php if(get_sub_field('answer_options')): ?>
<?php while(has_sub_field('answer_options')): ?> 
    <?php echo the_sub_field('answer'); ?>
<?php endwhile; ?> 
<?php endif; ?>

Why dont you change your approach slightly?

<?php 
   $rows = get_field('step_by_step_test');  // Get the test
   $question_count = get_field('select_number_of_questions');  // Get the number of questions
   $rows = shuffle($rows);   // Randomize your questions
   $rows = array_slice($rows, $question_count);   // Now set the array to only contain the number of questions you wanted
   foreach ($rows as $row) {
       echo $row['question'];    // Show the question
       if(get_sub_field('answer_options', $row['id'])) {
              while(has_sub_field('answer_options', $row['id'])) {
                       echo the_sub_field('answer');
              }
       }
   } 
?>

I made the assumption that you can alter "get_sub_field" to include the ID of the question, so you can then include the ID in your "where" field of "answer_options". This will allow you to link the question.

I think that what you need is to set up the whole thing in a loop. query by custom field

Or you could store the ids of the questions you got above, an then, below, query for the answers for those specific posts.

Here's how I randomized my WordPress slider using the Advanced Custom Fields plugin + Royal Slider with a modified version of TheSwiftExchange's code above

<div id="full-width-slider" class="royalSlider heroSlider rsMinW">

<?php
   /*
    *  Slider Repeater field shuffled
    *  http://stackoverflow.com/questions/12563116/incorporating-extra-loop-into-random-selection
    */


    $rows = get_field('slider');
//  For Debugging:
//  echo "<pre>";
//  var_dump($rows);
//  echo "</pre>";

    $quotes = get_field('slide_text');  // Get the number of images
    shuffle($rows);   // Randomize your slides

    foreach ($rows as $row) {

        $slideImageID =  $row['slide_image'];
        $slideImage = wp_get_attachment_image_src( $slideImageID, 'full' );
        $slideText = $row['slide_text'];
        ?>

        <div class="rsContent">

               <div class="royalCaption">
                   <div class="royalCaptionInner">

                       <div class="infoBlock">
                            <?php if(!empty($slideText)) {
                                   echo $slideText;
                           }; ?>
                       </div>
                   </div>
               </div>
               <img src="<?php echo $slideImage[0]; ?>" class="" />

        </div><!-- /.rsContent -->
    <?php }  // end foreach ?>

</div><!-- /slider-wrap -->