Turns out that the way I was doing it was correct

$value doesn't exist at end of method if inner loop is not run.