Yeah, I think PHP is a little quicky with arrays. Using $array[0] is not always the answer like in visual basic or javascript.
try a while loop with the $key and $value separated with a -> command.
So, for instance:
while($key->$value==0) {
Process PHP code
}
I do it this way and it works. Read up on the arrays.