Php 如何选择一组随机的行?

Php 如何选择一组随机的行?,php,wordpress,Php,Wordpress,如何选择一组随机的行 重要位: 我需要通过变量指定要选择的随机行数 例如,假设我想要选择的行数是10,那么它必须选择10个不同的行。我不想让它挑出同一行几次,直到它有10次 下面的代码选择了1个随机行,我如何根据上面的规范来定制它 <?php $rows = get_field('repeater_field_name'); $row_count = count($rows); $i = rand(0, $row_count - 1); echo $rows[$i]['sub_field

如何选择一组随机的行

重要位:

  • 我需要通过变量指定要选择的随机行数
  • 例如,假设我想要选择的行数是10,那么它必须选择10个不同的行。我不想让它挑出同一行几次,直到它有10次 下面的代码选择了1个随机行,我如何根据上面的规范来定制它

    <?php $rows = get_field('repeater_field_name');
    $row_count = count($rows);
    $i = rand(0, $row_count - 1);
    
    echo $rows[$i]['sub_field_name']; ?>
    

    只需在random row进程中循环您想要获得的随机行数

    <?php
    $rows_to_get=10;
    $rows = get_field('repeater_field_name');
    $row_count = count($rows);
    $x=0
    while($x<$rows_to_get){
        echo $rows[rand(0, $row_count - 1)]['sub_field_name'];
        $x++;
    }
    ?>
    

    您可以尝试一下

    $rows = get_field('repeater_field_name');
    var_dump(__myRand($rows, 10));
    
    function __myRand($rows, $total = 1) {
        $rowCount = count($rows);
        $output = array();
        $x = 0;
        $i = mt_rand(0, $rowCount - 1);
    
        while ( $x < $total ) {
            if (array_key_exists($i, $output)) {
                $i = mt_rand(0, $rowCount - 1);
            } else {
                $output[$i] = $rows[$i]['sub_field_name'];
                $x ++;
            }
        }
        return $output ;
    }
    
    $rows=get_field('repeater_field_name');
    var_dump(uu myRand($rows,10));
    函数_myRand($rows,$total=1){
    $rowCount=计数($rows);
    $output=array();
    $x=0;
    $i=mt_rand(0,$rowCount-1);
    而($x<$total){
    如果(数组\键\存在($i,$output)){
    $i=mt_rand(0,$rowCount-1);
    }否则{
    $output[$i]=$rows[$i]['sub_field_name'];
    $x++;
    }
    }
    返回$output;
    }
    
    一个简单的解决方案:

    $rows = get_field('repeater_field_name');
    $limit = 10;
    
    // build new array
    $data = array();
    foreach ($rows as $r) { $data[] = $r['sub_field_name']; }
    shuffle($data);
    $data = array_slice($data, 0, min(count($data), $limit));
    
    foreach ($data as $val) {
      // do what you want
      echo $val;
    }
    

    这个话题可能对你有用。比我的答案好得多,
    min
    位很棒。@SeanBright谢谢,看起来它很接近工作了。有几次它选择了同一行。。。有一次它甚至三次选择了相同的一个。你有多行具有相同的
    子字段名称吗?@SeanBright是的,我有,没有办法改变,因为它从Wordpress获取它。我添加了一个不同的实现,我认为更好。我还修改了最初的实现。
    
    $rows = get_field('repeater_field_name');
    var_dump(__myRand($rows, 10));
    
    function __myRand($rows, $total = 1) {
        $rowCount = count($rows);
        $output = array();
        $x = 0;
        $i = mt_rand(0, $rowCount - 1);
    
        while ( $x < $total ) {
            if (array_key_exists($i, $output)) {
                $i = mt_rand(0, $rowCount - 1);
            } else {
                $output[$i] = $rows[$i]['sub_field_name'];
                $x ++;
            }
        }
        return $output ;
    }
    
    $rows = get_field('repeater_field_name');
    $limit = 10;
    
    // build new array
    $data = array();
    foreach ($rows as $r) { $data[] = $r['sub_field_name']; }
    shuffle($data);
    $data = array_slice($data, 0, min(count($data), $limit));
    
    foreach ($data as $val) {
      // do what you want
      echo $val;
    }