混合两个阵列但不是随机播放?

时间:2013-11-12 10:13:00

标签: php arrays sorting merge shuffle

我想合并/混合两个数组,但我希望它随机“混合”,但不要改组。例如:

$first = array(1,2,3,4);
$second = array(10,20,30,40);

可能的“混音”是:

$merged = array(1,10,20,30,2,40,3,4);
$merged = array(10,1,2,20,30,3,4,40);
$merged = array(1,2,3,10,20,4,30,40);

请注意,如果我们将值取回,我们仍然会有原始订单:

1,2,3,4
10,20,30,40

在php中有快速的方法吗?

4 个答案:

答案 0 :(得分:0)

坎。也许这样的事情可能有用。

//Loop this shit until both arrays are done
$j = 0;
$i = 0;
$r = rand(0, 1);

if($r == 0) {
    $ret .= $array1[$i];
    $i++;
} else if($r == 1) {
    $ret .= $array2[$j];
    $j++;
}

当然,您必须在此代码中处理一些例外情况,但它可能是路径。

答案 1 :(得分:0)

嗯,还有(另一种)可能的方法:

$arr = call_user_func_array('array_merge', array_map(null, $first, $second));
print_r($arr); // [1, 10, 2, 20, 3, 30, 4, 40];

Demo。这显然是确定性的;对于随机排序,每一对应该另外洗牌。例如:

function zipShuffle($first, $second) {
  return call_user_func_array('array_merge', array_map(function($a, $b) {
    return mt_rand(0, 1) ? [$a, $b] : [$b, $a];
  }, $first, $second));
}

...但显然无法制作像[1,2,3,10,20...]这样的东西。如果需要,这是另一个解决方案:

function orderedShuffle($first, $second) {
  $results = [];
  $stor    = [$first, $second];
  $i       = mt_rand(0, 1);
  // switching between arrays on the fly
  while (list(, $v) = each($stor[$i])) {
    $results[] = $v;
    $i = mt_rand(0, 1);
  }

  // one array is gone, now we need to add all the elements
  // of the other one (as its counter left where it was)
  $i = 1 - $i;
  while (list(, $v) = each($stor[$i])) {
    $results[] = $v;
  }
  return $results;
}

Demo。实际上,最后一个函数很容易根据需要扩展到很多数组。

答案 2 :(得分:0)

你能试试吗,

    <?php 


        $first = array(1,2,3,4);
        $second = array(10,20,30,40);

        $arrayMixed=array();
        $firstReverse=array_reverse($first);
        $secondReverse=array_reverse($second);

        $firstReverseCount = count($firstReverse);
        $secondReverseCount = count($secondReverse);

        foreach($firstReverse as $key=>$val) {
            if ($firstReverseCount>0) { 

                    array_push($arrayMixed, array_pop($firstReverse));

                    if ($secondReverseCount>0) {
                        array_push($arrayMixed, array_pop($secondReverse));
                    }

            }
        }
        $ArrayMixeddata =  array_merge($arrayMixed, $second);       


        echo "<pre>";
            print_r($ArrayMixeddata);
        echo "</pre>";
?>

答案 3 :(得分:0)

不是很快,但它们有效。

    // with permutations
    function combineShuffleOrder($first, $second)
    {
        // combine into one array with alternation
        $firstLen = count($first);
        $secondLen = count($second);
        $max = max($firstLen, $secondLen);
        $result = array();
        for($i=0; $i < $max; $i++) {
            if ($i < $firstLen)
                $result[] = $first[$i];
            if ($i < $secondLen)
                $result[] = $second[$i];
        }

        // "shuffle" with permutation
        $len = count($result);
        for($i=1; $i<$len; $i++) {
            if (rand(1, 3) % 2 == 0) {
                $tmp = $result[$i-1];
                $result[$i-1] = $result[$i];
                $result[$i] = $tmp;
                $i++; // skip one exchange
            }
        }
        return $result;
    }

    // with using "shuffle" in subarray 
    function combineShuffleOrder2($first, $second)
    {
        // combine into one array with alternation
        $firstLen = count($first);
        $secondLen = count($second);
        $max = max($firstLen, $secondLen);
        $result = array();
        for($i=0; $i < $max; $i++) {
            $sub = array();
            if ($i < $firstLen)
                $sub[] = $first[$i];
            if ($i < $secondLen)
                $sub[] = $second[$i];
            shuffle($sub);
            $result = array_merge($result, $sub);
        }

        return $result;
    }

    // with using "shuffle" in subarray if sources arrays are equals by length
    function combineShuffleOrder3($first, $second)
    {
        $max = count($first);
        $result = array();
        for($i=0; $i < $max; $i++) {
            $sub = array(
                $first[$i],
                $second[$i]
            );
            shuffle($sub);
            $result = array_merge($result, $sub);
        }

        return $result;
    }

    $first = array(1,2,3,4);
    $second = array(10,20,30,40);
    print_r(combineShuffleOrder($first, $second));
    print_r(combineShuffleOrder2($first, $second));
    print_r(combineShuffleOrder3($first, $second));