0

I have written a simple php script running within drupal. It imports json from a different server and converts it to a multidimensional array. when I added a for-loop which rewrites the array a bit, the loading time increased from 1 second to 5 seconds. Am I doing something wrong here?

part of original "fast" code:

public function give_nausys_request_rew_array()  {
            $this->jsonarray = drupal_json_decode($this->give_nausys_request());
            $this->jsonarray = $this->jsonarray[reservations];

            return $this->jsonarray;

    }

part of "slow" code:

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;

    }

1 Answer 1

1

i don't know the average size of your array, but maybe the count function in php needs to loop through your array to perform the counting. So maybe you are actually looping through your array two times. Try maybe using foreach, and the php will handle loop for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.