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

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.
 
    $row = &$result[count($result)-1]


Of course this works, but as this thread ( http://stackoverflow.com/questions/3117604/why-is-calling-a-function-such-as-strlen-count-etc-on-a-referenced-value-so-s ) 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.