You don’t need any of that stuff. That’s only there to handle the possibility that text fields have HTML in them, and the $itemids[0] part is treating the ID as an array, which it isn’t.
You could paste the entire file or method with your latest changes… there’s a decent chance that the issue is just something simple, like a syntax error.