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 8196693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:34:07+00:00 2026-06-07T05:34:07+00:00

I am looping through several thousands of rows from a mysql result and adding

  • 0

I am looping through several thousands of rows from a mysql result and adding them to different arrays.

I get a reference to the last element in the array to operate on (for reasons outside the scope of this question).

I.e. (just an example of the scenario)

$myarray = array();
$result = &$myarray;
$row = &$result[count($result)-1]

Of course this works, but as this thread ( Why is calling a function (such as strlen, count etc) on a referenced value so slow? ) explains , calling a function which doesnt expect a variable by reference , With a variable by reference, causes the function to make a copy of the variable to operate on.

As my $result array grows to thousands of elements , continuously calling count on it , causing copies of it being made , makes the code unusable slow.

How can I get around this – Without suggesting to not use variables by reference.
Also, I cant keep separate rowcounters.

Basically I need to know how to dereference a by-reference variable (which I understand cant be done) , or some other way to get a array length ? , or a way to get a reference to the last row in an array (without popping it)

Bearing in mind the array could also be empty.

Already took me ages to find out that This is the problem , so I would really appreciate any help with solving the problem.

EDIT
To everyone that says that count cannot possibly be slower than end/key:
Just run this simple test to confirm

//setup array of 10 000 elements
$ar = array();
for ($i = 0 ; $i < 10000 ; $i++)
{
    $ar[] = $i;
}
//get a reference to it
$ref = &$ar;
error_log (date("Y/m/d H:i:s")." : speed test 1 \r\n",3,"debug.log");
//do 10 000 counts on the referenced array
for ($i = 0 ; $i < 10000 ; $i++)
{
    $size = count($ref);
}
error_log (date("Y/m/d H:i:s")." : speed test 2 \r\n",3,"debug.log");
//do 10 000 end/key on the referenced array
for ($i = 0 ; $i < 10000 ; $i++)
{
    reset($ref);
    end($ref);
    $size = key($ref);
}
error_log (date("Y/m/d H:i:s")." : end \r\n",3,"debug.log");

Output: 15 seconds to do the counts … less than a second to do the end/key

2012/07/10 17:25:38 : speed test 1
2012/07/10 17:25:53 : speed test 2
2012/07/10 17:25:53 : end
  • 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-06-07T05:34:08+00:00Added an answer on June 7, 2026 at 5:34 am

    I’m still not sure I understand why this is necessary. However, you’ve indexed the array as a numerical sequential array, so you can do this “trick” to get the size:

    // Make sure the array pointer is at the end of the array
    end($result); // Not sure if this is necessary based on your description
    $size = key($result) + 1; // Get the numeric key of the last array element
    

    Note that both end() and key() take their parameters by reference. However, I wouldn’t be surprised if they’re now operating on references of references, but this is something you can investigate.

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

Sidebar

Related Questions

I am looping through several hundred records from a XML file and inserting in
I'm looping through several items and making an ajax request for each of them
Looking through my VisualSVNServer logs, I'm seeing several attempts from different client IP addresses
I am looping through a list of samaccountnames and performing several actions: # Disabling
For the last hour I've tried several different solutions but none have solved my
I'm looking through the Vows documentation and in several places it uses the syntax
Looping through each id retrieve value of objects in 1 dimentional array and 2
While looping through a Dataset which code snippet should I use, should I go
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]
I've been looping through the contents of a list in the simple fashion to

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.