How to Combine two arrays randomly in PHP

arrays, php, random

Solution

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

Problem

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, ...); ```

Original source

Related problems