我已经编写了一个在drupal中运行的简单php脚本。它从不同的服务器导入json并将其转换为多维数组。当我添加一个for循环来稍微重写数组时,加载时间从1秒增加到5秒。我是不是做错了什么?
原始“快速”代码的一部分:
public function give_nausys_request_rew_array() {
$this->jsonarray = drupal_json_decode($this->give_nausys_request());
$this->jsonarray = $this->jsonarray[reservations];
return $this->jsonarray;
}“慢”代码的一部分:
public function give_nausys_request_rew_array() {
$this->jsonarray = drupal_json_decode($this->give_nausys_request());
$this->jsonarray = $this->jsonarray[reservations];
$this->arraycount = count($this->jsonarray)-1;
for ($i = 0; $i <= $this->arraycount; $i++) {
$this->jsonarray[$i][GUID] = $this->jsonarray[$i][yachtId].'-'.$this->jsonarray[$i][periodFrom].'-'.$this->jsonarray[$i][periodTo];
}
return $this->jsonarray;
}发布于 2015-08-28 05:35:12
我不知道数组的平均大小,但也许php中的count函数需要循环数组来执行计数。所以也许你实际上是在循环你的数组两次。尝试使用foreach,php将为您处理循环。
https://stackoverflow.com/questions/32258445
复制相似问题