如果你清楚你實際上想要達到的是什麼,它可能會有更多的幫助,但要繼續下去,這裏有一種方法可以獲得一個組的成員的所有匹配,而不需要匹配它從自己的組中的任何 - 我會假設你計劃有多個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種可能的組合。
如果chenage $ numberSet到:
$numberSet = array(array("a","b"),
array("c", "d", "e", "f"),
array("joe", "tom", "same")
);
結果:
A - C,A - d,A - E,A - F,A - 喬,一 - tom,a - 同樣的,b - c,b - d,b - e,b - f,b - joe,b - tom,b - 相同,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 - 同樣,f - a,f - b,f - joe,f - tom,f - 相同,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,
所以你想最終顯示所有可能的組合列表?或者你在尋找一組組合?或者你想要一個函數,傳遞一個ID並返回該ID的所有可能的組合? – Scoobler 2011-01-11 22:59:45
謝謝你的回覆!我想打印,因爲我將發送清單給HR感謝:) – isJustMe 2011-01-11 23:11:41