I’m using a PHP framework that returns SQL results as iteratable objects. Problem is I have a SQL query that that returns one row and I don’t want to have to create a foreach-loop to get at the first – and only – element.
So how do I do it?
These don’t work:
$obj->item(0)->propName;
$obj->next()->propName;
$obj[0]->propName;
Any ideas?
Assuming by “iterable”, you mean that the object implements the
Iteratorinterface, you can use$obj->current()to retrieve the current element, so$obj->current()->propNameis probably what you want.If the iterator pointer has been moved (for example, if it was used in a
foreach, which doesn’t reset the pointer), then you can call$obj->rewind()to set the pointer back to the first element before you call$obj->current().