Match Cards with PHP

There is a fairly popular program (I forgot the name) that generates triangles where there is a question or answer on each side, and each triangle fits together, so the answer to one triangle corresponds to the question of the other, and when correctly composed, it creates a large (usually a regular hexagon).

I am trying to write a script where $t is a 2D array containing maps:

 $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 ); 

What now needs to be done, say, for example, "T1-S1 corresponds to T2, T1-S2 corresponds to T3, T1-S3 corresponds to T4." I have tried, and what I still have is below:

 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++; } } } 

Note: the above code is for the simplified version, where all issues were "resolved", and it was just a coincidence with the two sides.

After running my code, I get this output:

 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 

The problem is that $row tells me only the side of the triangle, not the actual triangle. So my question is this:

How to make my script work so that it displays "ta-Sb matches Tc-Sd", where a is the triangle, b is the side, c is the triangle that matches, d is the side with which it matches, if we assume that in each array the side values ​​are in order?

Hope the question is clear, but feel free to ask any questions.

Furthermore, ideally, when it corresponds to Ta-Sb with Tc-Sd , it should not correspond to Tc-Sd with Ta-Sb . Is this also possible?

+4
source share
2 answers

It’s easier for me to approach these types of problems with objects, not with arrays. It's too hard to remember what each level of the array means and how they line up. So I could do something like this:

 <? // 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; } ?> 

Fill:

 <?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 ?> 

Now you can easily find out which triangle, side, question or coincidence you are facing, for example:

$ Polygon [2] β†’ Sides [1] β†’ state (means that the side of this triangle should not show the answer to the question)

$ Polygon [0] β†’ Sides [3] β†’ Question-> id (will contain the question identifier)

$ Polygon [1] β†’ corresponds to [2] (will hold the triangle key that corresponds to side 2 of polygon 1)

It may seem like a jump if you are not used to objects, but this is a pretty simple way to do this, you can simply treat them like illustrious arrays and forget about all the other objects that may exist at the moment.

After you fill them with values ​​- to get matches, you simply look at each polygon and display everything you need.

Hope this helps!

+1
source

Does it help?

 foreach ($t as $ti => $array) { foreach ($array as $ri => $row) { $i = 0; while ($i <= 4) { if(in_array($row, $t[$i])){ echo $row.' '.$ti.' '.$ri.' matches with triangle ' . $i . '<br />'; } $i++; } } } 
0
source

Source: https://habr.com/ru/post/1502095/


All Articles