首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php for循环数组重写性能

问php for循环数组重写性能
EN

Stack Overflow用户
提问于 2015-08-28 04:02:39
回答 1查看 62关注 0票数 0

我已经编写了一个在drupal中运行的简单php脚本。它从不同的服务器导入json并将其转换为多维数组。当我添加一个for循环来稍微重写数组时,加载时间从1秒增加到5秒。我是不是做错了什么?

原始“快速”代码的一部分:

代码语言:javascript
复制
public function give_nausys_request_rew_array()  {
            $this->jsonarray = drupal_json_decode($this->give_nausys_request());
            $this->jsonarray = $this->jsonarray[reservations];

            return $this->jsonarray;

    }

“慢”代码的一部分:

代码语言:javascript
复制
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;

    }
EN

回答 1

Stack Overflow用户

发布于 2015-08-28 05:35:12

我不知道数组的平均大小,但也许php中的count函数需要循环数组来执行计数。所以也许你实际上是在循环你的数组两次。尝试使用foreach,php将为您处理循环。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32258445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档