使用 3 个数组创建匹配

发布于 2024-10-11 16:24:26 字数 829 浏览 5 评论 0原文

我正在为我公司的拼车项目开发一个应用程序,但遇到了问题(该过程有点复杂)。我想做的是以下内容:

我有 3 个团队,每组 3 人,每个团队都有一个唯一的成员 id 例如:

组 1 = (1,2,3,4)

组 2 = (5,6,7,8 )

第 3 组 = (9,10,11,12)

这个想法是对 2 个成员进行尽可能多的组合(我认为每个成员至少有 8 个)不与来自同一组的人进行匹配。

例如

1-5 1-6 1-7 1-8 1-9 1-10 1-11 1-12 2-5 2-6 2-7 2-8 2-9 ... 等等

这是一个代码片段(它可能与我想要实现的目标没有意义,但我是一名初级程序员)

<?php

$numberSet = array( range(1,4),
                    range(5,8),
                    range(9,12) 
             ); 

$sizeofArray=count($numberSet); 

    for ($i=0; $i<$sizeofArray; $i++){
        for ($j=0; $j<count($numberSet[$i]); $j++){
            for ($k=0; $k<count($numberSet[$i]); $k++){
                echo $numberSet[$i][$j] . "<br>";
            }
        }   
    }   
?>

I have an issue with an application that I'm developing for a car pooling program in my company (the process is kind of complex). What I want to do is the follwoing:

I have 3 teams of 3 people, each team has a unique member id for instance:

Group 1 = (1,2,3,4)

Group 2 = (5,6,7,8)

Group 3 = (9,10,11,12)

The idea is to make as many combinations as possible of 2 members (I think it is at least 8 per member) without matching with someone from the same group.

For example

1-5
1-6
1-7
1-8
1-9
1-10
1-11
1-12
2-5
2-6
2-7
2-8
2-9
...
and so on

This is a code snippet (it might not have sense with what I want to achieve but I'm a junior programmer)

<?php

$numberSet = array( range(1,4),
                    range(5,8),
                    range(9,12) 
             ); 

$sizeofArray=count($numberSet); 

    for ($i=0; $i<$sizeofArray; $i++){
        for ($j=0; $j<count($numberSet[$i]); $j++){
            for ($k=0; $k<count($numberSet[$i]); $k++){
                echo $numberSet[$i][$j] . "<br>";
            }
        }   
    }   
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦途 2024-10-18 16:24:27

如果您明确了您真正想要实现的目标,可能会有更多帮助,但继续下去,这是一种获取一组成员的所有匹配项的方法,而不会将其与该组中的任何人匹配自己的组 - 我假设您计划在工作集中拥有多个 ID,而不是简单的 1234、5678、9 10 11 12:

    // Build an example array:
    $numberSet = array( range(1,4),
                        range(5,8),
                        range(9,12) ); 

    // The function will return an array of matches when passed the array and the ID:
    function findCombos($id, $set)
    {
        // Store the matches found:
        $matches = array();
        // Loop through each array in the multidimensional array which was passed:
        foreach ($set as $group)
        {
            // Make sure the ID passed isn't a member of the current array, don't want its matches:
            if (!in_array($id, $group))
            {
                // Loop through each array as the ID isn't a member of this group:
                foreach ($group as $member)
                {
                    // Add the match the the matches array:
                    $matches[] = $member;
                }
            }
        }
        // Pass the matches back:
        return $matches;
    }

最后寻找单个用户匹配:

    // Find all the matches for ID 2 from the multidimensional array:
    $matches = findCombos("2", $numberSet);
    // Display the nubmer of matches:
    echo "Found ".count($matches)." matches for 2.<br/>";
    // Loop through each match found:
    foreach ($matches as $match)
    {
        // Display the results:
        echo "2 - ".$match."<br/>";
    }

结果:

Found 8 matches for 2.
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
2 - 11
2 - 12

如果您想显示所有可能性,您可以做一些事情像这样:

    $count = 0;
    foreach ($numberSet as $group)
    {
        foreach ($group as $member)
        {
            $matches = findCombos($member, $numberSet);   
            $count = $count+count($matches);      
            foreach ($matches as $match)
            {
                echo $member." - ".$match.", ";
            }
        }
    }
    echo "<br/>Found ".$count." possible combinations.";

结果:

1 - 5、1 - 6、1 - 7、1 - 8、1 - 9、1 -
10, 1 - 11, 1 - 12, 2 - 5, 2 - 6, 2 -
7, 2 - 8, 2 - 9, 2 - 10, 2 - 11, 2 -
12、3 - 5、3 - 6、3 - 7、3 - 8、3 - 9、
3 - 10、3 - 11、3 - 12、4 - 5、4 - 6、
4 - 7, 4 - 8, 4 - 9, 4 - 10, 4 - 11, 4
- 12, 5 - 1, 5 - 2, 5 - 3, 5 - 4, 5 - 9, 5 - 10, 5 - 11, 5 - 12, 6 - 1, 6 -
2、6 - 3、6 - 4、6 - 9、6 - 10、6 -
11、6 - 12、7 - 1、7 - 2、7 - 3、7 -
4、7 - 9、7 - 10、7 - 11、7 - 12、8 -
1、8 - 2、8 - 3、8 - 4、8 - 9、8 - 10、
8 - 11, 8 - 12, 9 - 1, 9 - 2, 9 - 3, 9
- 4, 9 - 5, 9 - 6, 9 - 7, 9 - 8, 10 - 1, 10 - 2, 10 - 3, 10 - 4, 10 - 5, 10
- 6, 10 - 7, 10 - 8, 11 - 1, 11 - 2, 11 - 3, 11 - 4, 11 - 5, 11 - 6, 11 -
7、11 - 8、12 - 1、12 - 2、12 - 3、12
- 4、12 - 5、12 - 6、12 - 7、12 - 8、

找到 96 种可能的组合。

如果将 $numberSet 更改为:

   $numberSet = array( array("a","b"),
                array("c", "d", "e", "f"),
                array("joe", "tom", "same") 
         );

结果:

a - c、a - d、a - e、a - f、a - 乔、a
- 汤姆,a - 相同,b - c,b - d,b - e,b - f,b - 乔,b - 汤姆,b - 相同,c -
a、c - b、c - 乔、c - 汤姆、c - 相同、
d - a、d - b、d - 乔、d - 汤姆、d -
同样,e - a,e - b,e - joe,e - 汤姆,
e - 相同、f - a、f - b、f - 乔、f -
汤姆,f - 相同,乔 - a,乔 - b,乔 -
c、乔-d、乔-e、乔-f、汤姆-a、
汤姆-b,汤姆-c,汤姆-d,汤姆-e,
汤姆 - f,相同 - a,相同 - b,相同 - c,
相同 - d,相同 - e,相同 - f,

If you clear up what it is you actually want to achieve, it maybe a little more help, but to be going on with, here is one way to get all the matches for a member of one group, without matching it to anyone from its own group - I will assume you plan on having multiple ID's and not a simple 1234, 5678, 9 10 11 12 in your working set:

    // Build an example array:
    $numberSet = array( range(1,4),
                        range(5,8),
                        range(9,12) ); 

    // The function will return an array of matches when passed the array and the ID:
    function findCombos($id, $set)
    {
        // Store the matches found:
        $matches = array();
        // Loop through each array in the multidimensional array which was passed:
        foreach ($set as $group)
        {
            // Make sure the ID passed isn't a member of the current array, don't want its matches:
            if (!in_array($id, $group))
            {
                // Loop through each array as the ID isn't a member of this group:
                foreach ($group as $member)
                {
                    // Add the match the the matches array:
                    $matches[] = $member;
                }
            }
        }
        // Pass the matches back:
        return $matches;
    }

Finally looking for a single users matches:

    // Find all the matches for ID 2 from the multidimensional array:
    $matches = findCombos("2", $numberSet);
    // Display the nubmer of matches:
    echo "Found ".count($matches)." matches for 2.<br/>";
    // Loop through each match found:
    foreach ($matches as $match)
    {
        // Display the results:
        echo "2 - ".$match."<br/>";
    }

Results:

Found 8 matches for 2.
2 - 5
2 - 6
2 - 7
2 - 8
2 - 9
2 - 10
2 - 11
2 - 12

If you wanted to show all possibilities you could do something like this:

    $count = 0;
    foreach ($numberSet as $group)
    {
        foreach ($group as $member)
        {
            $matches = findCombos($member, $numberSet);   
            $count = $count+count($matches);      
            foreach ($matches as $match)
            {
                echo $member." - ".$match.", ";
            }
        }
    }
    echo "<br/>Found ".$count." possible combinations.";

Results:

1 - 5, 1 - 6, 1 - 7, 1 - 8, 1 - 9, 1 -
10, 1 - 11, 1 - 12, 2 - 5, 2 - 6, 2 -
7, 2 - 8, 2 - 9, 2 - 10, 2 - 11, 2 -
12, 3 - 5, 3 - 6, 3 - 7, 3 - 8, 3 - 9,
3 - 10, 3 - 11, 3 - 12, 4 - 5, 4 - 6,
4 - 7, 4 - 8, 4 - 9, 4 - 10, 4 - 11, 4
- 12, 5 - 1, 5 - 2, 5 - 3, 5 - 4, 5 - 9, 5 - 10, 5 - 11, 5 - 12, 6 - 1, 6 -
2, 6 - 3, 6 - 4, 6 - 9, 6 - 10, 6 -
11, 6 - 12, 7 - 1, 7 - 2, 7 - 3, 7 -
4, 7 - 9, 7 - 10, 7 - 11, 7 - 12, 8 -
1, 8 - 2, 8 - 3, 8 - 4, 8 - 9, 8 - 10,
8 - 11, 8 - 12, 9 - 1, 9 - 2, 9 - 3, 9
- 4, 9 - 5, 9 - 6, 9 - 7, 9 - 8, 10 - 1, 10 - 2, 10 - 3, 10 - 4, 10 - 5, 10
- 6, 10 - 7, 10 - 8, 11 - 1, 11 - 2, 11 - 3, 11 - 4, 11 - 5, 11 - 6, 11 -
7, 11 - 8, 12 - 1, 12 - 2, 12 - 3, 12
- 4, 12 - 5, 12 - 6, 12 - 7, 12 - 8,

Found 96 possible combinations.

If you chenage $numberSet to:

   $numberSet = array( array("a","b"),
                array("c", "d", "e", "f"),
                array("joe", "tom", "same") 
         );

The result:

a - c, a - d, a - e, a - f, a - joe, a
- tom, a - same, b - c, b - d, b - e, b - f, b - joe, b - tom, b - same, c -
a, c - b, c - joe, c - tom, c - same,
d - a, d - b, d - joe, d - tom, d -
same, e - a, e - b, e - joe, e - tom,
e - same, f - a, f - b, f - joe, f -
tom, f - same, joe - a, joe - b, joe -
c, joe - d, joe - e, joe - f, tom - a,
tom - b, tom - c, tom - d, tom - e,
tom - f, same - a, same - b, same - c,
same - d, same - e, same - f,

少女七分熟 2024-10-18 16:24:27

您可能想看看 array_diff()。我可以看到类似这样的工作:

$everyone=range(1,12);
$groups=array(range(1,4), range(5,8), range(9,12));
$cnt=count($groups);
for($i=0;$i<$cnt;$i++) {
    // this will give you all the people who aren't in your group
    $diff=array_diff($everyone,$groups[$i]);

    // loop and compare here 
}

我不清楚的是“1-5”和“5-1”对是否相同,即您需要它们是唯一的对。

You might want to take a look at array_diff(). I could see something like this working:

$everyone=range(1,12);
$groups=array(range(1,4), range(5,8), range(9,12));
$cnt=count($groups);
for($i=0;$i<$cnt;$i++) {
    // this will give you all the people who aren't in your group
    $diff=array_diff($everyone,$groups[$i]);

    // loop and compare here 
}

What wasn't clear to me is if the pair "1-5" and "5-1" are the same or not i.e. you need them to be unique pairs.

温折酒 2024-10-18 16:24:27

如果它只计算 2 的对(而不是更高),您可以简单地计算其他两个数组。

对于 array1 中的任何人,只需 count(array2) + count(array3) = 对数

if it's only calculating pairs of 2 (and no higher), you can simply count the other two arrays.

for anyone in array1, simply count(array2) + count(array3) = number of pairs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文