I think you need to re-instate the is_array check - if I remember correctly sometimes $val was NOT an array