douheng8629 2013-07-13 19:01 采纳率: 100%
浏览 16
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • douzhi4830 2013-07-13 21:18
    关注

    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
            )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作