如何在PHP中随机组合两个数组

时间:2021-06-18 12:15:25

How to combine two arrays into single one and i am requesting this in such a way that the 3rd combination array should contains one value from one array and the next one from other array and so on.. or ( it could be random) ex:

如何将两个数组组合成一个数组,而我的请求是这样的:第三个组合数组应该包含一个数组中的一个值,另一个数组中的一个值等等。或者(可能是随机的)例:

$arr1 = (1, 2, 3, 4, 5);
$arr2 = (10, 20, 30, 40, 50);

and combined array

并结合数组

$arr3 = (1, 10, 2, 20, 3, 30, ...);

5 个解决方案

#1


6  

I also made a function for fun that will produce the exact output you had in your question. It will work regardless of the size of the two arrays.

我还做了一个有趣的函数,它会产生你刚才问的结果。不管两个数组的大小如何,它都可以工作。

function FosMerge($arr1, $arr2) {
    $res=array();
    $arr1=array_reverse($arr1);
    $arr2=array_reverse($arr2);
    foreach ($arr1 as $a1) {
        if (count($arr1)==0) {
            break;
        }
        array_push($res, array_pop($arr1));
        if (count($arr2)!=0) {
            array_push($res, array_pop($arr2));
        }
    }
    return array_merge($res, $arr2);
}

#2


15  

If it can be random, this will solve your problem:

如果它可以是随机的,这将解决你的问题:

$merged = array_merge($arr1, $arr2);
shuffle($merged);

#3


3  

This will return a random array:

这将返回一个随机数组:

$merged = array_merge($arr1,$arr2);
shuffle($merged);

#4


1  

sort($arr3 = array_merge($arr1, $arr2));

array_merge() will merge your arrays into one. sort() will sort the combined array.

array_merge()将把数组合并到一个数组中。sort()将对组合数组进行排序。

If you want it random instead of sorted:

如果你想要随机而不是排序:

shuffle($arr3 = array_merge($arr1, $arr2));

$arr3 contains the array you're looking for.

$arr3包含您要查找的数组。

#5


0  

You can use

您可以使用

<?php
arr3 = array_merge ($arr1 , $arr2 );
print_r(arr3);
?>

which will output in

将输出

$arr3 = (1,2,3,4,5,10,20,30,40,50)

#1


6  

I also made a function for fun that will produce the exact output you had in your question. It will work regardless of the size of the two arrays.

我还做了一个有趣的函数,它会产生你刚才问的结果。不管两个数组的大小如何,它都可以工作。

function FosMerge($arr1, $arr2) {
    $res=array();
    $arr1=array_reverse($arr1);
    $arr2=array_reverse($arr2);
    foreach ($arr1 as $a1) {
        if (count($arr1)==0) {
            break;
        }
        array_push($res, array_pop($arr1));
        if (count($arr2)!=0) {
            array_push($res, array_pop($arr2));
        }
    }
    return array_merge($res, $arr2);
}

#2


15  

If it can be random, this will solve your problem:

如果它可以是随机的,这将解决你的问题:

$merged = array_merge($arr1, $arr2);
shuffle($merged);

#3


3  

This will return a random array:

这将返回一个随机数组:

$merged = array_merge($arr1,$arr2);
shuffle($merged);

#4


1  

sort($arr3 = array_merge($arr1, $arr2));

array_merge() will merge your arrays into one. sort() will sort the combined array.

array_merge()将把数组合并到一个数组中。sort()将对组合数组进行排序。

If you want it random instead of sorted:

如果你想要随机而不是排序:

shuffle($arr3 = array_merge($arr1, $arr2));

$arr3 contains the array you're looking for.

$arr3包含您要查找的数组。

#5


0  

You can use

您可以使用

<?php
arr3 = array_merge ($arr1 , $arr2 );
print_r(arr3);
?>

which will output in

将输出

$arr3 = (1,2,3,4,5,10,20,30,40,50)