加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

与PHP匹配卡

发布时间:2020-12-13 22:50:02 所属栏目:PHP教程 来源:网络整理
导读:有一个非常受欢迎的程序(我忘了名字),它生成三角形,每边都有一个问题或答案,每个三角形组合在一起,使得一个三角形上的答案与另一个三角形上的问题匹配,并且当正确放在一起时它会产生更大的形状(通常是正六边形). 我正在尝试编写一个脚本,其中$t是一个包含卡
有一个非常受欢迎的程序(我忘了名字),它生成三角形,每边都有一个问题或答案,每个三角形组合在一起,使得一个三角形上的答案与另一个三角形上的问题匹配,并且当正确放在一起时它会产生更大的形状(通常是正六边形).

我正在尝试编写一个脚本,其中$t是一个包含卡片的2D数组:

$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与Tc-Sd匹配,它就不应该与Ta-Sb匹配Tc-Sd.这也可能吗?

解决方法

我发现用对象而不是数组来处理这些类型的问题更容易.记住每个阵列级别的含义以及它们如何排列太复杂了.所以我可能会这样做:

<?
// I say Polygon instead of triangle because ideally the logic should scale for squares,octagons,anything! But start with triangles of course.
class Polygon{ 
  var $Sides = array(); // Side objects - there should be 3 of them for a triangle
  var $matches = array(); // holds the ids of the matching polygonn - keys line up with $Sides

  function __construct(){
    $Sides[0] = new Side();
    $Sides[1] = new Side();
    $Sides[2] = new Side();
  }

}

class Side{
  var $Question; // Question object
  var $state; // 'q' or 'a' - does this side show the question or answer?

  function __construct(){
    $Question = new Question();
  }

}

class Question{
  var $id; // database id of the question
  var $question;
  var $answer;
}
?>

要填充:

<?php

$Triangle[0]=new Polygon();
$Triangle[0]->Side[0]->Question->id=1;
$Triangle[0]->Side[0]->Question->question='Yo momma serves more requests than what?';
$Triangle[0]->Side[0]->Question->answer='HTTP';
$Triangle[0]->Side[0]->state='q'; // This side shows the question
$Triangle[0]->matches[0]= 4; // Side 0 of this triangle matches a side of triangle 4

// write a loop that does this for all triangles using whatever your logic is for matching them up

?>

现在,您可以通过以下方式轻松了解您正在处理的三角形,边形,问题或匹配:

$Polygon [2] – > Sides [1] – > state(表示该三角形的一侧应显示答案而不是问题)

$Polygon [0] – > Sides [3] – > Question-> id(将保存问题的ID)

$Polygon [1] – >匹配[2](将保持与多边形1的第2边匹配的三角形的键)

如果你不习惯对象,它可能会感觉像是一个飞跃,但这是一个非常简单的方法,你可以像对待美化数组一样对待它们,忘掉对象现在可以做的所有其他东西.

在用值填充它们之后 – 为了获得匹配,您只需循环遍历每个Polygon并输出您需要的任何内容.

希望这可以帮助!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读