简体   繁体   English

尝试在数组中创建网格图会在PHP中返回致命内存错误

[英]Trying to create a grid map in an array returns a fatal memory error in php

I am working on a random RPG Map generator that get's inserted into the database for later retrieval. 我正在使用随机RPG地图生成器,该生成器已插入数据库中以供以后检索。 Basically the user will enter some minor details on the form and specify how many tiles of each terrain they need to have on the map. 基本上,用户将在表单上输入一些次要细节,并指定他们需要在地图上具有每个地形的图块数量。 Then the tiles are randomly inserted into the map and database. 然后,将图块随机插入到地图和数据库中。 I insert the tiles into an Array and then shuffle the results. 我将图块插入Array中,然后重新排列结果。 I do a loop through the array and loop through and X & Y grid to insert the terrain data. 我遍历数组并遍历X和Y网格以插入terrain数据。 But everytime I do that, I get a memory error: Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 32 bytes) This is my code: 但是每次这样做,我都会遇到一个内存错误: 致命错误:耗尽的268435456字节允许的内存大小(尝试分配32个字节)这是我的代码:

$gridarray = array();

for ($jungle1 = 0; $jungle1 <= $jungle -1; $jungle1++) {
array_push($gridarray,"jungle");
}

for ($swamp1 = 0; $swamp1 <= $swamp -1; $swamp1++) {
array_push($gridarray,"swamp");
}

for ($cave1 = 0; $cave1 <= $cave -1; $cave1++) {
array_push($gridarray,"cave");
}

for ($mountain1 = 0; $mountain1 <= $mountain -1; $mountain1++) {
array_push($gridarray,"mountain");
}
for ($ocean1 = 0; $ocean1 <= $ocean -1; $ocean1++) {
array_push($gridarray,"ocean");
}

for ($volcanic1 = 0; $volcanic1 <= $volcanic -1; $volcanic1++) {
array_push($gridarray,"volcanic");
}

for ($desert1 = 0; $desert1 <= $desert -1; $desert1++) {
array_push($gridarray,"desert");
}

for ($dirt1 = 0; $dirt1 <= $dirt -1; $dirt1++) {
array_push($gridarray,"dirt");
}

for ($forest1 = 0; $forest1 <= $forest -1; $forest++) {

array_push($gridarray,"dirt");
}

for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic++) {

array_push($gridarray,"arctic");
}

for ($grass1 = 0; $grass1 <= $grass -1; $grass++) {

array_push($gridarray,"grass");
}

echo '<table cellspacing="0" cellpadding="0" border="0">';
$gridsquare = $xsize * $ysize;

$grid = 0;
shuffle($gridarray);



for ($x = 1; $x <= $xsize; $x++) {
    echo '<tr>';
          for ($y = 1; $y <= $ysize; $y++) {

           $terrain = $gridarray[$grid];
           $terrain_img = 'http://www.sw-bfs.com/images/grids/' . $terrain . '.png';

$query2 = " INSERT INTO terrain ( ent_id, ent_type, grid_img, grid_type, grid_exit_e, grid_exit_w, grid_exit_s, grid_exit_n, grid_exit_u, grid_exit_d, x, y)  VALUES ( '$loc_id', 'terrain', '$terrain_img', '$terrain','1', '1', '1', '1', '0', '0', '$x', '$y') "; 

 $result2 = mysql_query($query2) or die(mysql_error());

echo '<td><img src="' . $terrain_img . '" title="' . $terrain . '"/></td>';

$grid++;



} 
echo '</tr>';


} 
echo "</table>";

It is telling me that the error is occurring on the line that reads like this: 它告诉我,错误发生在如下所示的行上:

array_push($gridarray,"arctic");
}

Obviously I am doing something wrong or at least I am not writing the code efficiently enough. 显然我做错了什么,或者至少我没有足够有效地编写代码。 Forgive me, I am still learning. 原谅我,我还在学习。 Can someone provide some better assistance? 有人可以提供更好的帮助吗? Thank you in advance. 先感谢您。

Never mind. 没关系。 This: 这个:

for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic++) {

array_push($gridarray,"arctic");

Is missing a 1 in $arctic++. $ arctic ++中缺少1。 Should be like this: 应该是这样的:

for ($arctic1 = 0; $arctic1 <= $arctic -1; $arctic1++) {

array_push($gridarray,"arctic");

Now works perfectly. 现在可以完美运行了。 Feel free to use my snippet above to create random maps for the database. 随意使用上面的代码片段为数据库创建随机映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM