I made a 2 dimensional array which checks if 3 of the same values are equal to each other. My problem is that when the code detects 3 in a row it changes values, but it doesn’t update to the original array ($aww_ar).
Is there a way to do this?
Code:
<?php $aww_ar = array(); echo"<table>"; for($y = 0; $y < 6; $y++){ echo"<tr>"; for($x = 0; $x < 6; $x++){ $randomise = rand(1,4); $aww_ar[$x][$y] = "<td>".$randomise."</td>"; echo $aww_ar[$x][$y]; } echo"</tr>"; } echo"</table>"; $counter = 0; for ($h=0; $h < count($aww_ar); $h++) { for ($u=0; $u < count($aww_ar[0]); $u++) { if($aww_ar[$h][$u] == $aww_ar[$h][$u-1] && $aww_ar[$h][$u] == $aww_ar[$h][$u+1]){ $counter++; $aww_ar[$h][$u] = 'X'; $aww_ar[$h][$u+1] = 'X'; $aww_ar[$h][$u-1] = 'X'; echo $aww_ar[$h][$u]; echo $aww_ar[$h][$u+1]; echo $aww_ar[$h][$u-1]; } } } echo $counter;
Advertisement
Answer
Ive worked it out a bit with the help of Lopes Wang
<?php $aww_ar = array(); // Create array for($y = 0; $y < 9; $y++){ for($x = 0; $x < 9; $x++){ $randomise = rand(1,4); $aww_ar[$x][$y] = "<td>".$randomise."</td>"; } } // Detects matches and marks them with X foreach ($aww_ar as $key=>&$value) { foreach ($aww_ar[0] as $k=>$v) { if ($k > 0 && $aww_ar[$key][$k] == $aww_ar[$key][$k - 1] && $aww_ar[$key][$k] == $aww_ar[$key][$k + 1]) { $aww_ar[$key][$k] = 'X'; $aww_ar[$key][$k + 1] = 'X'; $aww_ar[$key][$k - 1] = 'X'; } } } echo"<table border='2px;'>"; for($y = 0; $y < 9; $y++){ echo"<tr>"; for($x = 0; $x < 9; $x++){ echo "<td>" .$aww_ar[$x][$y]."</td>"; } echo"</tr>"; } echo"</table>";