将循环减少为一个 - 仅限TIPS

the*_*3RV 2 php performance reduce loops for-loop

重写下面的代码,使它只包含一个循环,只使用一个itterational变量.

$result = Array();
for ($x = 0; $x < 6; $x++){
    for ($y = 0; $y < 6; $y++){
        for ($z = 0; $z < 6; $z++){
            $result[$x][$y][$z] = $x * $y * $z;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

没有答案,请指出我在正确的方向.有没有可以处理这个问题的php类?或者它只是一个简单的问题解决技术?或者这是一个技巧问题?

推荐结果如下:

$result = Array();
for ($i = 0; $i < 216; $i++){
    $x = $i % 6;
    $y = floor($i/6) % 6;
    $z = floor($i/36);
    $result[$x][$y][$z] = $x * $y * $z;
}
Run Code Online (Sandbox Code Playgroud)

Rud*_*udu 8

缺少家庭作业的旗帜?

在基数6中思考,并运行从零到6 ^ 3(216)的循环,然后使用mod减少/转换回x y z位置.