我想要在一個三維數(shù)組中,我只想繪制數(shù)組外部的數(shù)組單元格,如下圖所示。我想通過函數(shù)color()獲取一個數(shù)組(通過引用調(diào)用)將數(shù)組的外部 Rubix 轉(zhuǎn)換為 1 并將內(nèi)部 Rubix 轉(zhuǎn)換為 0我創(chuàng)建了函數(shù) 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; } }}如何通過三維數(shù)組解決著色魔方???
1 回答

子衿沉夜
TA貢獻1828條經(jīng)驗 獲得超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 關(guān)注
- 110 瀏覽
添加回答
舉報
0/150
提交
取消