随机交错/压缩平面数组,而不会丢失原始数组的元素顺序


Randomly interleave/zipper flat arrays without losing element order from original arrays

我想将两个数组合并/混合在一起,但我希望它随机"混合",但不要洗牌。 例如:

$first = [1, 2, 3, 4];
$second = [10, 20, 30, 40];

可能所需的"混合物"是:

  • [1, 10, 20, 30, 2, 40, 3, 4]
  • [10, 1, 2, 20, 30, 3, 4, 40]
  • [1, 2, 3, 10, 20, 4, 30, 40]

请注意,如果我们提取值,我们仍将具有以下原始顺序:

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

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

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

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

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;
}

演示。最后一个函数实际上很容易根据需要扩展任意数量的数组。

你能试试这个吗,

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

不是快速的方法,但它们有效。

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

我建议形成两个输入数组的单个数组,以便更轻松地切换。 只需循环并使用随机选择的数组中的一个元素,然后将该选择推送到结果数组中。 当数组池减少到单个数组时,终止循环并将最后一个幸存数组的剩余元素附加到结果数组中。

我将使用一个包含四个数组的池(一个从一开始就是空的)来证明该代码片段足够健壮,可以处理可变数量的数组、每个数组中可变数量的元素和空数组。

代码:(演示)

$first = [1, 2, 3, 4];
$second = [10, 20, 30, 40];
$third = [];
$fourth = ['a', 'b'];
$pool = [$first, $second, $third, $fourth];
$result = [];
while (count($pool) > 1) {
    $pullFrom = array_rand($pool);
    if (!$pool[$pullFrom]) {
        unset($pool[$pullFrom]);
        continue;
    }
    $result[] = array_shift($pool[$pullFrom]);
}
var_export(array_merge($result, ...$pool));
<小时 />

或者没有array_merge()count()调用,但它可以对array_shift()进行更多迭代调用:(演示)

$pool = [$first, $second, $third, $fourth];
$result = [];
while ($pool) {
    $pullFrom = array_rand($pool);
    if (!$pool[$pullFrom]) {
        unset($pool[$pullFrom]);
        continue;
    }
    $result[] = array_shift($pool[$pullFrom]);
}
var_export($result);

循环此脚本,直到两个数组都完成。

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

当然,您必须在此代码中处理一些异常,但它可能是路由。