php Shuffle多维数组,同时保持列顺序

时间:2017-10-24 08:28:35

标签: php arrays shuffle

我有一个多维数组如下:

$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');

到目前为止一直很好 - 现在我的数组按“出现”列按升序排序。

现在出现了问题。我想随机改组数组,但仍然按升序维持“出现”列

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

3 个答案:

答案 0 :(得分: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));

答案 1 :(得分:1)

在我看来,这些其他答案都是过度设计的(难怪你不能跟随他们的行为)。您只需要两个函数调用shuffle()然后usort()。根本没有必要对它们进行分组。 (我将在我的演示中使用“太空飞船”操作员,但如果您的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',
  ),
)

P.S。按appeared DESC排序就像在$a之后交换$breturn一样简单。

答案 2 :(得分:0)

<?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);