this generates unneeded memory overhead (think about larger documents). what do you think about:

````php
if(isset($document[0]) && isset($document[count($document)-1])) ...
````

ass a more ressource friendly solution?