PHP如何将多维数组写入文件

hgb*_*bso 0 php

我正在尝试使用文件来为checkers保存一个数组

这是阵列

$board = array(
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0),
         array(0,0,0,0,0,0,0,0,0)
   );
Run Code Online (Sandbox Code Playgroud)

同时还提供值,以便我可以设置棋盘的开头,将棋子置于预定位置以开始游戏,然后让用户输入他们想要将棋子移动到哪个位置

我已经有了这个while循环

      $row = 0;
    print "<form>";
    print "<table border = 1>";
    while ($row < 8){ // Counts to 8. (from 0...7 = 8 times. 0 ... 8 = 9 times)
       print "<tr>";
       $row++;
       $col = 0; // reset column to 0 each time printing one row.

       while ($col < 8){
        print "<td>";
        if($Board[$row][$col] == 0)
        {
            $value=$row.$col;
            print "<input type=\"checkbox\" name=\"box[]\" value=\"$value\">";
            // Add \ before " otherwise it will treat as the end of the quote.

        }
        print "</td>";
        $col++;

       }

       print "</tr>";

    }
    print "</table>";
    print "</form>";
Run Code Online (Sandbox Code Playgroud)

}

小智 10

file_put_contents($f, serialize($board));
Run Code Online (Sandbox Code Playgroud)

这将在文件中序列化多维数组.

要读回来,请使用

$board = unserialize(file_get_contents($f));
Run Code Online (Sandbox Code Playgroud)