【问题标题】:php Shuffle multi-dimensional array whilst maintaining column orderphp Shuffle 多维数组,同时保持列顺序
【发布时间】:2017-10-24 08:28:35
【问题描述】:

我有一个多维数组如下:

$lee[] = array("question" => "3", "appeared" => "0");
$lee[] = array("question" => "4", "appeared" => "1");
$lee[] = array("question" => "5", "appeared" => "0");
$lee[] = array("question" => "6", "appeared" => "0");
$lee[] = array("question" => "7", "appeared" => "1");
$lee[] = array("question" => "8", "appeared" => "1");
$lee[] = array("question" => "9", "appeared" => "2");
$lee[] = array("question" => "10", "appeared" => "0");
$lee[] = array("question" => "12", "appeared" => "3");
$lee[] = array("question" => "15", "appeared" => "3");
$lee[] = array("question" => "19", "appeared" => "3");


function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
    $sort_col = array();
    foreach ($arr as $key=> $row) {
        $sort_col[$key] = $row[$col];
    }

    array_multisort($sort_col, $dir, $arr);
}

array_sort_by_column($lee, 'appeared');

到目前为止一切顺利 - 现在我的数组按“出现”列升序排序。

现在问题来了。我想随机打乱数组,但仍以升序保持“出现”列

如何做到这一点?我不知道。。

【问题讨论】:

  • 您想要随机播放数组的“部分”。所以将数组分成几部分,洗牌每个部分并合并它们
  • 所以你想打乱数组的“问题”部分,但保持出现的顺序不变?您尝试过的一个简短示例以及最终结果应该如何的一个最小示例会很好。
  • 你应该创建 2 个不同的数组。
  • 谁能给我一个例子来说明如何做到这一点?

标签: php arrays shuffle


【解决方案1】:
$lee[] = array("question" => "3", "appeared" => "0");
$lee[] = array("question" => "4", "appeared" => "1");
$lee[] = array("question" => "5", "appeared" => "0");
$lee[] = array("question" => "6", "appeared" => "0");
$lee[] = array("question" => "7", "appeared" => "1");
$lee[] = array("question" => "8", "appeared" => "1");
$lee[] = array("question" => "9", "appeared" => "2");
$lee[] = array("question" => "10", "appeared" => "0");
$lee[] = array("question" => "12", "appeared" => "3");
$lee[] = array("question" => "15", "appeared" => "3");
$lee[] = array("question" => "19", "appeared" => "3");

$parts = [];
// Group items by `appeared` value
foreach ($lee as $item) {
    $parts[$item['appeared']][] = $item;
}

// shuffle each group
foreach ($parts as &$part) {
    shuffle($part);
}

// sort array by keys in ascending order
ksort($parts);
// merge groups to one array
print_r(call_user_func_array('array_merge', $parts));

【讨论】:

    【解决方案2】:

    在我看来,这些其他答案都被过度设计了(难怪你不能遵循他们的做法)。你只需要两个函数调用,shuffle() 然后usort()。根本没有必要将它们分组。 (我将在演示中使用“spaceship”运算符,但如果您的 php 版本需要,您可以使用任何老式的比较技术。)

    代码:(Demo)

    shuffle($lee);  // shuffle the array
    usort($lee,function($a,$b){return $a['appeared']<=>$b['appeared'];});  // sort shuffled array by appeared value
    var_export($lee);
    

    可能的输出:

    array (
      0 => 
      array (
        'question' => '10',
        'appeared' => '0',
      ),
      1 => 
      array (
        'question' => '5',
        'appeared' => '0',
      ),
      2 => 
      array (
        'question' => '6',
        'appeared' => '0',
      ),
      3 => 
      array (
        'question' => '3',
        'appeared' => '0',
      ),
      4 => 
      array (
        'question' => '4',
        'appeared' => '1',
      ),
      5 => 
      array (
        'question' => '8',
        'appeared' => '1',
      ),
      6 => 
      array (
        'question' => '7',
        'appeared' => '1',
      ),
      7 => 
      array (
        'question' => '9',
        'appeared' => '2',
      ),
      8 => 
      array (
        'question' => '12',
        'appeared' => '3',
      ),
      9 => 
      array (
        'question' => '15',
        'appeared' => '3',
      ),
      10 => 
      array (
        'question' => '19',
        'appeared' => '3',
      ),
    )
    

    附言通过appeared DESC 订购就像在return 之后交换$a$b 一样简单。

    【讨论】:

      【解决方案3】:
      <?php
          $lee[] = array("question" => "3", "appeared" => "0");
          $lee[] = array("question" => "4", "appeared" => "1");
          $lee[] = array("question" => "5", "appeared" => "0");
          $lee[] = array("question" => "6", "appeared" => "0");
          $lee[] = array("question" => "7", "appeared" => "1");
          $lee[] = array("question" => "8", "appeared" => "1");
          $lee[] = array("question" => "9", "appeared" => "2");
          $lee[] = array("question" => "10", "appeared" => "0");
          $lee[] = array("question" => "12", "appeared" => "3");
          $lee[] = array("question" => "15", "appeared" => "3");
          $lee[] = array("question" => "19", "appeared" => "3");
      
          function array_sort_by_column(&$arr, $col, $dir = SORT_ASC) {
              $sort_col = array();
              foreach ($arr as $key=> $row) {
                  $sort_col[$key] = $row[$col];
              }
      
              array_multisort($sort_col, $dir, $arr);
          }
          array_sort_by_column($lee, 'appeared');
          foreach( $lee as $row){
              $question[] = $row['question'];
          }
          shuffle($question);
      
          for($i=0;$i<count($lee);$i++){
              $lee[$i]['question'] = $question[$i];
          }
          echo "<pre>";print_r($lee);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-23
        • 1970-01-01
        • 2011-12-14
        • 1970-01-01
        • 2018-03-31
        • 2016-05-14
        • 2012-06-22
        • 2015-11-02
        相关资源
        最近更新 更多