我如何在 php 中通过三维数组给 Rubix Cube 着色

我想要在一个三维数组中,我只想绘制数组外部的数组单元格,如下图所示。

https://img4.mukewang.com/64d5f6e20001550c02430455.jpg

我想通过函数color()获取一个数组(通过引用调用)将数组的外部 Rubix 转换为 1 并将内部 Rubix 转换为 0

https://img3.mukewang.com/64d5f6eb0001ce6001950435.jpg

我创建了函数 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;

        }

    }

}

如何通过三维数组解决着色魔方???


大话西游666
浏览 53回答 1
1回答

子衿沉夜

<?phpfunction color(&$ls) {&nbsp; &nbsp; foreach ($ls as $xs => $xl) {&nbsp; &nbsp; &nbsp; &nbsp; foreach ($xl as $ys => $yl) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($yl as $zs => $cell) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $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);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP