Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP算法,打印每个x、y、z坐标的值_Php_Algorithm_Loops - Fatal编程技术网

PHP算法,打印每个x、y、z坐标的值

PHP算法,打印每个x、y、z坐标的值,php,algorithm,loops,Php,Algorithm,Loops,我有这样一种情况,我知道输出应该是什么,但想不出怎么做: 我有三个坐标,x,y,z $x = 2; $z = count(range(0, 1)); //this will result in number 2 but I need the values to be displayed when printing inside the loop like below. $y = 2; 现在,我需要进行8次for循环2x2x2并打印这些值: 1 1 0 2 1 0 2 0 1 2 2 0 2

我有这样一种情况,我知道输出应该是什么,但想不出怎么做:

我有三个坐标,x,y,z

$x = 2;
$z = count(range(0, 1)); //this will result in number 2 but I need the values to be displayed when printing inside the loop like below.
$y = 2; 
现在,我需要进行8次for循环2x2x2并打印这些值:

1 1 0
2 1 0
2 0 1
2 2 0
2 1 2
1 1 1
2 1 1
2 2 1
我开始这样做,但不确定这是否是一个正确的开始:

$get_total_spots = $z * $x* $y;

for ($i = 0; $i <= $get_total_spots ; $i++) {
  //at least I know using this loop it will iterate 8 times
}

不太清楚您的确切意思,但如果您想打印rubics立方体每个框的所有位置x、y、z,假设值为0、1或2,您可以制作3个循环:

foreach范围0,2为$x{ foreach范围0,2为$y{ foreach范围0,2为$z{ echo$x$y$z; } } } 结果:

0 0 0
0 0 1
0 0 2
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 2 2
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 2 2
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 1 2
2 2 0
2 2 1
2 2 2

其中每一行代表一个方形的rubics立方体,这看起来很相似。我不明白range的用法,所以我可能把这一切都搞错了

$spot = fn() => random_int(0, 2);

for ($i = 0; $i < 8; $i++) {
    echo sprintf('%d %d %d%s', $spot(), $spot(), $spot(), PHP_EOL);
}

如果你能解释一下你是如何得到这些看似任意的数字的,那将是非常有用的。。它们是基于某种方式使用z x和y的公式吗?@Laurens是的,基本上,想象一个Rubicon,其中的每个正方形代表3个值x,y,z,因此,对于它们中的每一个,我需要有x,y,z坐标。我怎么能这么做?!这些数字有多大?如果它们足够小,您可以使用3个for循环来打印values@pew007它可以比我展示的更大,甚至更小。谢谢,我明天会尝试一下,现在太累了。值可能会非常不同。Z始终是两个数字的范围,可以是-6到+8。那么像你贴的那样的循环是否仍然是一样的呢?对不起,这在我的例子中不起作用,因为坐标一直在变化,所以它可以是不同大小的矩形。它并不总是一个立方体,那么请更详细地解释一下它到底是你想要的。由于你提供的信息数量有限,没有人能帮助你。这里有一个字面上的意思:谢谢,但在我的情况下,任何情况下都不能是随机的。这些都是一些坐标-对于rubiconI的每一个正方形来说都是唯一的,实际上,他们根本不知道你们想做什么。
1 2 1
0 2 0
0 2 1
2 0 0
2 1 2
2 0 0
1 2 0
1 1 2