我想要在一个三维数组中,我只想绘制数组外部的数组单元格,如下图所示。我想通过函数color()获取一个数组(通过引用调用)将数组的外部 Rubix 转换为 1 并将内部 Rubix 转换为 0我创建了函数 color() 但它不起作用魔方的墙不会改变$matrix = [ [ [3, 5, 13, 56], [0, 1, 165, 1], [-8, 78, 5, 8], [6, 5, 23, 45] ], [ [1, 17, 5, 3], [1, 5, 1, 65], [6, 5, 5, -4], [0, 4, 3, 90] ], [ [9, 9, 8, 0], [3, 5, 4, 8], [0, 5, 3, 9], [1, 4, 5, 7] ]];function color(&$matrix){ for ($i = 0; $i < count($matrix); ++$i) { echo 'layer ' . ($i + 1) . ':' . PHP_EOL; foreach ($matrix as $j) { if($i == 1){ $y=0; foreach ($j as $k) { if($y == 0 ) echo $f = 1 . ' '; elseif($y == 1) echo $f = 0 . ' '; elseif($y == 2) echo $f = 1 . ' '; $y++; } }else{ foreach ($j as $k) { echo $k = 1 . ' '; } } echo PHP_EOL; } }}如何通过三维数组解决着色魔方???
1 回答
子衿沉夜
TA贡献1828条经验 获得超3个赞
<?php
function color(&$ls) {
foreach ($ls as $xs => $xl) {
foreach ($xl as $ys => $yl) {
foreach ($yl as $zs => $cell) {
$ls[$xs][$ys][$zs] = (int)($xs == 0 || $xs == count($ls) - 1 || $ys == 0 || $ys == count($xl) - 1 or $zs == 0 or $zs == count($yl) - 1);
}
}
}
}
- 1 回答
- 0 关注
- 67 浏览
添加回答
举报
0/150
提交
取消