0

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

$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

4 回答 4

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

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

于 2013-11-12T10:18:29.213 回答
0

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

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

演示。最后一个函数实际上很容易根据需要扩展为尽可能多的数组。

于 2013-11-12T10:31:24.063 回答
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>";
?>
于 2013-11-12T10:35:37.260 回答
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));
于 2013-11-12T10:38:25.887 回答