有一个非常流行的程序(我忘记了名字),它生成三角形,每边都有一个问题或答案,每个三角形组合在一起,使得一个三角形的答案与另一个三角形的问题相匹配,并且正确组合在一起它创建了一个更大的形状(通常是一个正六边形)。
我正在尝试编写一个脚本,其中$t
包含卡片的二维数组:
$t = array();
// $t['1'] represents the 'center' triangle in this basic example
$t['1'] = array(
'1', // One side of T1, which is an answer
'3-1', // Another side, this is a question
'2+1' // Final side, another question
);
// These cards go around the outside of the center card
$t['2'] = array(
'2-1' // This is a question on one side of T2, the other sides are blank
);
$t['3'] = array(
'2' // This is an answer on one side of T3, the other sides are blank
);
$t['4'] = array(
'3' // This is an answer on one side of T4, the other sides are blank
);
现在需要它做的,例如,“T1-S1 匹配 T2,T1-S2 匹配 T3,T1-S3 匹配 T4”。我已经尝试过了,到目前为止我所拥有的如下:
foreach ($t as $array) {
foreach ($array as $row) {
$i = 0;
while ($i <= 4) {
if(in_array($row, $t[$i])){
echo $row . ' matches with triangle ' . $i . '<br />';
}
$i++;
}
}
}
注意:上面的代码是一个简化版本,其中所有问题都“解决”了,它只是匹配两侧。
运行我的代码后,我得到以下输出:
1 matches with triangle 1
1 matches with triangle 2
2 matches with triangle 1
2 matches with triangle 3
3 matches with triangle 1
3 matches with triangle 4
1 matches with triangle 1
1 matches with triangle 2
2 matches with triangle 1
2 matches with triangle 3
3 matches with triangle 1
3 matches with triangle 4
问题是,$row
这只告诉我三角形的边,而不是实际的三角形。所以我的问题是:
如何使我的脚本工作,以便它输出“Ta-Sb 与 Tc-Sd 匹配”,其中 a 是三角形,b 是边,c 是它匹配的三角形,d 是它匹配的边,假设在每个数组中边的值是有序的?
我希望问题很清楚,但是请随时提出任何问题。
此外,理想情况下,一旦匹配Ta-Sb with Tc-Sd
,它不应该匹配Tc-Sd with Ta-Sb
。这也可能吗?