【问题标题】:php for-loop array rewriting performancephp for-loop 数组重写性能
【发布时间】:2015-08-27 20:02:38
【问题描述】:

我编写了一个在 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;

    }

【问题讨论】:

    标签: php arrays drupal drupal-7


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2019-10-25
      相关资源
      最近更新 更多