均匀地将桶中的项目(或具有属性)分配给属性之后的另一个桶(或阵列)

Evenly Distribute items in a bucket( or having a property ) to another bucket(or array) following property ...

I have an array like

 Array
(
    [type1] => Array
        (
            [0] => 1,
            [1] => 2,
            [2] => 3,
            [3] => 4
        )

    [type2] => Array
        (
            [0] => 5,
            [1] => 6,
            [2] => 7,
            [3] => 8
        )

    [type3] => Array
        (
            [0] => 9,
            [1] => 10,
            [2] => 11,
            [3] => 12
        )
    [type4] => Array
        (
            [0] => 13,
            [1] => 14,
            [2] => 15,
            [3] => 16
        )
)

SO i need to sort it into an array such that each element of this new array will have all the types of elements equally distributed to each of the elements of new array

What i Need

    array(
[0]=>array(1,5,9,13)
[1]=>array(2,6,10,14)
[2]=>array(3,7,11,15)
[3]=>array(4,8,12,16)
)

The issue here is the first array's sub-arrays may have varying no of elements

A simpler version: Imagine u have a set of horses from different countries How do you organize a "n" ( here 4 ) number of different races so that in all the races the country of the horse present is as evenly distributed as possible

In Db its something like

regid nation    race1
1     country1  1
2     country2  1
3     country3  0
4     country1  1
5     country1  1

where regid is id no for each participant and race1 is to denote if its participating in that race or not

The following generates two arrays: $values contains the values, and $out contains keys in the form type1:3. The variable $max determines the maximum number of sets:

$arr['type1'] = array(1, 2, 3, 4, 5);
$arr['type2'] = array(6, 7, 8, 9);
$arr['type3'] = array(10, 11, 12, 13, 14, 15);
$arr['type4'] = array(16, 17, 18, 19);
$arr['type5'] = array(20, 21, 22, 23, 24);
$max = 5;
$out = array();
$values = array();
$i = 0;
foreach ($arr as $key1 => $type){
    foreach ($type as $key2 => $item){
        $out[$i%$max][] = $key1.':'.$key2;
        $values[$i%$max][] = $item;
        $i++;
    }
}
var_dump($values);

[0] => array(1, 6, 11, 16, 21)
[1] => array(2, 7, 12, 17, 22)
[2] => array(3, 8, 13, 18, 23)
[3] => array(4, 9, 14, 19, 24)
[4] => array(5, 10, 15, 20)

Or you can access the array keys:

var_dump($out);

Array
(
    [0] => Array
        (
            [0] => type1:0
            [1] => type2:0
            [2] => type3:1
            [3] => type4:0
            [4] => type5:1
        )

    [1] => Array
        (
            [0] => type1:1
            [1] => type2:1
            [2] => type3:2
            [3] => type4:1
            [4] => type5:2
        )

    [2] => Array
        (
            [0] => type1:2
            [1] => type2:2
            [2] => type3:3
            [3] => type4:2
            [4] => type5:3
        )

    [3] => Array
        (
            [0] => type1:3
            [1] => type2:3
            [2] => type3:4
            [3] => type4:3
            [4] => type5:4
        )

    [4] => Array
        (
            [0] => type1:4
            [1] => type3:0
            [2] => type3:5
            [3] => type5:0
        )

)