in reply to Flip-flop reset?

You could make sure that your last array element always ends with a newline before processing it, to use your existing code:

$arr[-1] .= "\n";