下面是我用来将地图数组“翻译”为 SQL 代码的一些代码,以便在更新游戏地图时可以轻松更新数据库。正如您所看到的,它将 SQL 代码打印到屏幕上,以便我可以复制并粘贴它。
随着我的地图变得更大,这将变得效率低下,因为它会因大量输出而使浏览器崩溃,所以我想知道是否可以让它创建一个 .txt 文件并将所有数据写入其中而不是打印到屏幕?
<?php
if (isset($_POST['code'])){
$map = $_POST['code'];
$map = preg_replace("/,\\s*}/i", "}", $map);
$map = str_replace("{", "[", $map);
$map = str_replace("}", "]", $map);
$map = json_decode('[' . $map . ']');
$arrayCount1 = 0;
$arrayCount2 = -1;
$H = sprintf('%05d', 00000);
$V = sprintf('%05d', 00000);
$id = 1;
echo "INSERT INTO `map` (`id`, `horizontal`, `verticle`, `image`) VALUES" . "<br />";
for ($count1 = 0; $count1 < sizeof($map[0]); $count1++){
$arrayCount2++;
$arrayCount1 = 0;
$V = sprintf('%05d', $V + 1);
$H = sprintf('%05d', 00000);
for ($count2 = 0; $count2 < sizeof($map); $count2++){
echo "(" . $id . ", '" . $H . "', '" . $V . "', '" . $map[$arrayCount1][$arrayCount2] . "')," . "<br />";
$arrayCount1++;
$id++;
$H = sprintf('%05d', $H + 1);
}
}
}
?>