Time/Space complexity of PHP Array Time/Space complexity of PHP Array php php

Time/Space complexity of PHP Array


Because it acts like a hash table, you will have O(1) time when accessing an element by a key.

If you are looping through the array, naturally you will have O(n) time.

If you have time, you can actually check out PHP's implementation of array here


Accessing and iterating is describe by @Mike-Lewis so far

  • Setting a value: O(1)
  • Append: O(1) (Its the same as setting a value to the key "length")
  • Prepend: O(n) (Its a guess, but should fit, because it should rewrite the existing keys)
  • Unset: O(1)

Anything missed?


In addition to what @Mike Lewis said, I would add, that one array element in PHP occupies minimum of 52 bytes (proof)