$my_var is going to have to be used and done with before the end of each iteration of the loop... unless you make it an array to store the current value. Inside the loop $i is going to be the most direct method of accessing sort order.
Knowing something about what you want to accomplish with this is what will let us advise on a practical approach.