【发布时间】:2019-08-04 12:08:18
【问题描述】:
我将给定标记的坐标作为字符串,我需要将其转换为二维数组,第一个索引是 x 值数组,第二个索引是 y。
对于每个标记,坐标字符串如下所示:
$strCoordinatesMarker1 = "0,0,12:0,43,4";
上述示例的输出仅针对单个标记将是
$coordinatesMarker 1 = Array
(
[0] => Array
(
[0] => 0
[1] => 0
[2] => 12
)
(
[1] => Array
(
[0] => 0
[1] => 43
[2] => 4
)
)
文件输出一个包含所有标记和每个坐标数据的数组
到目前为止,我的代码在此方面运行良好
// Looping through the query of markers from the DB
// $strCoordinates is returned for each:
while ($stmt->fetch()) {
$xyArray = explode(':', $strCoordinates);
$coordinates = array(explode(',', $xyArray[0]), explode(',', $xyArray[1]));
$completeCoordinates[] = array('coordinates' => $coordinates);
}
这适用于小型数据集,我最近得到了一个有 300 个标记的数据集,每个标记有超过 3500 个坐标。
在这个数据集上尝试我的代码我得到了错误:
PHP Fatal error: Allowed memory size of 134217728 bytes exhausted
(tried to allocate 72 bytes)
所以我的问题是:会有更有效的方法在 php 中进行这种解析吗?记忆。我想确保我的代码尽可能地发挥作用,然后才可能增加内存,而我真的不想为这个大型数据集做这件事。
【问题讨论】:
-
要问的更好的问题是:您真的需要将所有 3500 个坐标同时保存在内存中吗?如果没有,请丢弃您不再需要的数据
标签: php string memory-leaks coordinates explode