php数组块分发重复

时间:2014-05-27 15:26:15

标签: php arrays duplicates distributed chunks

我想订购平衡群体的人,但这些人属于一个团队。我想知道是否可以生成群组,会阻止来自同一群组的更多群体属于同一群组。

示例数组(people => team)

$total = array(
    'Kitten01'  => 'A',
    'Kitten02'  => 'A',
    'Kitten03'  => 'U',
    'Kitten04'  => 'U',
    'Kitten05'  => 'B',
    'Kitten06'  => 'B',
    'Kitten07'  => 'M',
    'Kitten08'  => 'M',
    'Kitten09'  => 'C',
    'Kitten10'  => 'C',
    'Kitten11'  => 'Y',
);

我使用过这个功能:

function partition( $list, $p ) {
    $listlen = count( $list );
    $partlen = floor( $listlen / $p );
    $partrem = $listlen % $p;
    $partition = array();
    $mark = 0;
    for ($px = 0; $px < $p; $px++) {
        $incr = ($px < $partrem) ? $partlen + 1 : $partlen;
        $partition[$px] = array_slice( $list, $mark, $incr );
        $mark += $incr;
    }
    return $partition;
}

最后我生成了小组:

$max_group  = 8;
$people     = count($total);
$groups     = ceil($people / $max_group);

print_r(partition($total, $groups)) ;   

..并返回此信息:

Array
(
    [0] => Array
        (
            [Kitten01] => A
            [Kitten02] => A
            [Kitten03] => U
            [Kitten04] => U
            [Kitten05] => B
            [Kitten06] => B
        )

    [1] => Array
        (
            [Kitten07] => M
            [Kitten08] => M
            [Kitten09] => C
            [Kitten10] => C
            [Kitten11] => Y
        )

)

可以退货吗?:

 Array
(
    [0] => Array
        (
            [Kitten01] => A
            [Kitten03] => U
            [Kitten05] => B               
            [Kitten07] => M
            [Kitten09] => C
            [Kitten11] => Y 
        )

    [1] => Array
        (
            [Kitten02] => A
            [Kitten04] => U
            [Kitten06] => B                
            [Kitten08] => M                
            [Kitten10] => C
        )

)

我希望你的帮助

** 编辑:解决方案 **

  1. 使用William的功能 flip(),我会对每个团队的所有项目进行分类。

  2. 使用 make_group()功能,每个团队只需要一个Kitten,然后创建一个组,下次使用make_group()函数时,我会接受其余的。< / p>

  3. 最后,团队按团队G1(A,U,B,M,C,...),G2(A,U,B,M,C,......)顺序排序...... < / p>

  4. 如果我合并所有这些群组,我按比例分配(如果超过每组允许的最大数量:8)我将永远不会有两个人同一个团队:

  5. ** 最终代码 **

    # groups quantity
    $max_group      = 8;
    $people         = count($total);
    $groups         = ceil($people / $max_group);
    $total          = flip($total);
    $total_group    = array();
    
    # merge groups
    for($y=0;$y<$groups;$y++)
    {
        $total_group = array_merge($total_group, make_group($total));
    }
    
    # .. and divide proportionally
    print_r(partition($total_group, $groups));  
    

1 个答案:

答案 0 :(得分:1)

以下应该有用,我相信:

// Your input array
$total = array(
    'Kitten01'  => 'A',
    'Kitten02'  => 'A',
    'Kitten03'  => 'U',
    'Kitten04'  => 'U',
    'Kitten05'  => 'B',
    'Kitten06'  => 'B',
    'Kitten07'  => 'M',
    'Kitten08'  => 'M',
    'Kitten09'  => 'C',
    'Kitten10'  => 'C',
    'Kitten11'  => 'Y',
);

// Helper function that flips values and keys, so that your array
// would look like:
//     array(
//         'A' => array('Kitten01', 'Kitten02'),
//         'U' => array('Kitten03', 'Kitten04'),
//         'B' => array('Kitten05', 'Kitten06'),
//          ...
function flip($array) {
    $result = array();

    foreach ($array as $k => $v) {
        $result[$v][] = $k;
    }

    return $result;
}

// Make a group from a set of remaining members in teams
function make_group(&$teams) {
    $group = array();

    // Pick one member per team
    foreach ($teams as $k => &$v) {
        // If that team still has members, remove the member from the
        // team and add it to the group
        if ($member = array_shift($v)) {
            $group[$member] = $k;
         }
    }

    return $group;
}

$teams = flip($total);
// Repeat as needed
print_r(make_group($teams));
print_r(make_group($teams));