19. Nov 2021 |

Does PHP Loop Through the Whole Array to Find a Specific Element?

Same question here but I find its answer a bit misleading. My answer would be YES. You need to loop through the whole array to find a specific element and the worst case complexity is O(n). Unless:

  • you use associative array and ask the element by its key. Then the search takes constant time O(1).
  • you ask element by its index (you know where the element is and you go there directly). Then the search takes constant time O(1).

Leave a Reply

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.