Reputation: 850
This is more of a best practices/design question.
I'm creating a little browser game that involves a dynamically sized grid (think minesweeper). Each cell of the grid has data associated with it which I'm currently storing in a 3- or 4-dimensional array.
I'm not sure exactly how or when, but at some point I started running into memory limit issues (with a 128M limit).
In general the bulk of the data is stored in an array that looks something like this:
$values[$r][$c]['id'] = $id;
$values[$r][$c]['value'] = $value;
$values[$r][$c]['input'] = $value;
$values[$r][$c]['bgColor'] = bgColor($r, $c);
$values[$r][$c]['color'] = contrastingColor($values[$r][$c]['bgColor']);
$values[$r][$c]['weight'] = 'normal';
$values[$r][$c]['style'] = 'normal';
$values[$r][$c]['font'] = 'Arial';
$values[$r][$c]['fSize'] = '100%';
$values[$r][$c]['markerPaths'] = array();
$values[$r][$c]['deletedMarkerPaths'] = array();
where $r and $c represent the row and column of a cell. For reference here's the URL: http://www.deckbuildangaems.net/misc/gridgame/
I see an "easy" bit of memory optimization in that I could store the row and column as a single string and parse it every time I need it, though I'd love to avoid that. However in general, I feel like my tinkerish experience in PHP has hit its limit without having to take that next huge step in learning more advanced techniques and optimizations.
So I guess my superficial question is whether there's a simple way to keep my neatly structured and easily accessible array and still cut down on resource usage, or if I need to look into other storage formats. And my underlying concern is, if this isn't really feasible, then where should I go from here to put on my big boy pants? Or is the answer to that simply: "away from PHP"?
Thanks for any advice, == Matt
Upvotes: 1
Views: 1000
Reputation: 10630
Reaching the PHP memory limit should not be a concern. PHP stores all arrays in a Hash Table internally. From a best practices point of view, you really don't want to have many thousands of bytes of data in memory, instead, place it in a database of some sort or even a flat file. Even so, you should not be running into memory limits with just the use-case you described.
Most issues with hitting memory are due to bugs. PHP itself is very efficient at handling data, normal use-cases wont reach the it's limits. I would, however, recommend using a database system, even something like SQLite (which has very low overhead).
Upvotes: 1