Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:08:26+00:00 2026-05-31T18:08:26+00:00

I iterate over an array of arrays and access the array’s value through associative

  • 0

I iterate over an array of arrays and access the array’s value through associative keys, this is a code snippet. Note: i never iterate over the total array but only with a window of 10.

//extract array from a db table (not real code)
$array = $query->executeAndFetchAssociative;

$window_start = 0;

for($i = $window_start; $i<count($array) && $i<$window_start+10; $i++)
  echo($entry["db_field"]);

This is a sort of paginator for a web interface. I receive the windows_start value and display hte next 10 values.

A conceptual execution:

  1. Receive the windows_start number
  2. Start the cycle entering the window_start-TH array of the outer array
  3. Display the value of a field of the inner array via associative index
  4. Move to window_start+1

The inner arrays have about 40 fields. The outer array can grow a lot as it rapresent a database table.
Now i see that as the outer array gets bigger the execution over the windows of 10 takes more and more time.

I need some “performance theory” on my code:

If I enter the values of inner arrays via numeric key can I have better performance? In general is quickier accessing the array values with numeric index than accessing with associative index (a string)?

How does it cost entering a random entry ($array[random_num]) of an array of length N ? O(N), O(N/2) just for example

Finally the speed of iterating over an array depends on the array lenght? I mean i always iterate on 10 elements of the array, but how does the array lenght impact on my fixed length iteration?

Thanks
Alberto

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:08:28+00:00Added an answer on May 31, 2026 at 6:08 pm

    If I enter the values of inner arrays via numeric key can I have
    better performance? In general is quicker accessing the array values
    with numeric index than accessing with associative index (a string)?

    There might be a theoretical speed difference for integer-based vs string-based access (it depends on what the hash function for integer values does vs the one for string values, I have not read the PHP source to get a definite answer), but it’s certainly going to be negligible.

    How does it cost entering a random entry ($array[random_num]) of an
    array of length N ? O(N), O(N/2) just for example

    Arrays in PHP are implemented through hash tables, which means that insertion is amortized O(1) — almost all insertions are O(1), but a few may be O(n). By the way, O(n) and O(n/2) are the same thing; you might want to revisit a text on algorithmic complexity.

    Finally the speed of iterating over an array depends on the array
    length? I mean i always iterate on 10 elements of the array, but how
    does the array length impact on my fixed length iteration?

    No, array length is not a factor.

    The performance drops not because of how you access your array but because of the fact that you seem to be loading all of the records from your database just to process 10 of them.

    You should move the paging logic to the database itself by including an offset and a limit in your SQL query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since now, I'm using this loop to iterate over the elements of an array,
What's the best way to iterate over an array of arrays? sounds = [
I iterate over an array of CartItem class I created and populate a view
I want to iterate over an array of inputs that belong to certain class
How do I iterate over a Java array of variable length. I guess I
Which of the following orderings of nested loops to iterate over a 2D array
I got an array which I iterate over and try to create a variable.
I have a 3D array in Python and I need to iterate over all
I have the array: example = ['foo', 'bar', 'quux'] I want to iterate over
I have two arrays of objects. I would like iterate over each of these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.