使用php从数据库中选择随机名称列表

时间:2016-03-31 01:55:52

标签: php mysql

我是PHP新手,对PHP和MySQL有疑问。 我有大约150个名单,每个月我必须安排他们到新组,但必须与上个月不同。

那么如何使用PHP调用MySQL表中的所有名称并生成大约10个组并确保列表名称与上个月不一样

enter image description here

1 个答案:

答案 0 :(得分:0)

假设您已将所有姓名都列在名为$names的列表中。

$groups = [];

while(count($names) > 0)
{
    $group = [];

    while(count($names) > 0 && count($group) < 10)
    {
        $keys = array_keys($names);
        $index = $keys[rand(0, count($keys) - 1)];

        $group[] = $names[$index];
        unset($names[$index]);
    }

    $groups[] = $group;
}