php - How to get the length of a (by reference) array -


I am looping through thousands of lines from a mysql result and adding them to various arrays.

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

Ie (just one example of the scenario)

  $ myarray = array (); $ Result = & amp; $ Myarray; $ Line = & amp; $ Result [count ($ result) -1]   

Of course this works, but this thread tells (), calling a function that does not expect a variable from context, references With a variable from, the function is due to copy the variable to operate.

Thousands of elements become incremented as my $ resultary array, continuously counting it, making copies thereof, this code can not be deactivated.

How can I get around this - except for the suggestion of not using the variable according to the reference, I can keep separate rowcounters

Actually I know this How would the confusion in context (which I can not understand), or is there some other way to get the array length? , Or a method of finding the last line of reference in an array (without popping it)

The effect in the array can be empty.

I got the age to figure out that this is a problem, so I appreciate any help to solve the problem.

Edit tells everyone that the counting may not be slower than the end: just run this simple test to confirm

  // Setup of 10 000 elements ARR = AR = array (); For {$ i = 0; $ i <10000; $ i ++} {$ ar [] = $ i; } // Get a reference to it $ ref = & amp; $ Ar; Error_log (date ("Y / m / dh: i: s"). ": Speed ​​test 1 \ r \ n", 3, "debug.log"); On the referenced array for $ ($ I = 0; $ i & lt; 10000; $ i ++) / do do 10 000 calculations {$ size = count ($ ref); } Error_log (date ("Y / m / dh: i: s"). ": Speed ​​test 2 \ r \ n", 3, "debug.log"); // do 10 000 end / key on the referenced array for $ i ($ i = 0; $ i & lt; 10000; $ i ++); {reset ($ ref); End ($ ref); $ Size = key ($ ref); } Error_log (date ("Y / m / dh: i: s"). ": End \ r \ n", 3, "debug.log");   

Output: 15 seconds to calculate ... less than one second to 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    

I still can not believe that I understand why this is necessary. However, you have indexed the array as a numerical sequential array, so that you can "move" to get the shape:

  // Make sure that the end of the array indicator In the array is the end ($ result); // Not sure that your description is $ size = key ($ result) + 1 based on this; // Find the numerical key of the last element element   

Note that both take their parameters according to the context and. However, I would not be surprised that they are now working on references to contexts, but there is something that you can investigate.

Comments