简体   繁体   English

如何使用php / for loop扫描平方矩阵?

[英]How can i scan over a square matrices using php / for loop?

I need to write some code using some for-loops which tests the values of some array data. 我需要使用一些用于测试某些数组数据值的for循环编写一些代码。

1st pass: checking the following... 第一阶段:检查以下内容...

{x0,y0}

2nd pass: checking the 4 bits of data... 第二遍:检查数据的4位...

{x0,y0}, {x1,y0}, 
{x0,y1}, {x1,y1}.

3rd pass: checking 9 bits of data... 第三遍:检查9位数据...

{x0,y0}, {x1,y0}, {x2,y0}, 
{x0,y1}, {x1,y1}, {x2,y1},
{x0,y2}, {x1,y2}, {x2,y2}.

My little brain doesn't seem to want to function to get this answered. 我的小脑子似乎不想发挥作用以得到答案。 Can anyone help? 有人可以帮忙吗?

<?php

for ($pass = 0; $pass < count($matrix); $pass++)
{
    for ($i = 0; $i <= $pass; $i++)
    {
        for ($j = 0; $j <= $pass; $j++)
        {
            checkbit($matrix[$i][$j]);
        }
    }   
}

?>

Maybe something like this? 也许是这样的吗?

You could start with a simple function that operates on the matrix: 您可以从在矩阵上运行的简单函数开始:

function scan($x, $y) {...}

Additionally the canvas has a range for x and y, the starting number and it's ending number: 另外,画布的范围是x和y,起始编号和结束编号:

range x/y: 0,0/0,0

or later on: 或以后:

range x/y: 0,2/0,2

If you say that the ranges always start at 0 , and both ranges always have the same upper value, this can be reduced as one variable: $range . 如果您说范围始终从0开始,并且两个范围始终具有相同的上限值,则可以将其减少为一个变量: $range You can then just iterate over the matrix easily ( Demo ): 然后,您可以轻松地遍历矩阵( Demo ):

$range = 2;

foreach(range(0, $range) as $y)
    foreach(range(0, $range) as $x)
        scan($x, $y);

function scan($x, $y)
{
...
}

As $range depends on the number of the current pass, you can specify the maximum number of passes in $passes and iterate over it changing $range based on $pass ( Demo ): 作为$range取决于电流经过的天数,您可以指定最大遍数$passes ,并遍历它改变$range基础上$pass演示 ):

$passes = 3;

foreach(range(1, $passes) as $pass)
{
    $range = $pass-1;
    foreach(range(0, $range) as $y)
        foreach(range(0, $range) as $x)
            scan($x, $y);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM