Does PHP (5.4.x) allow one to create custom iterators that can be dropped into foreach loops ? (I have seen an iterator example that was fairly clunky)
I am accessing a web API that only allows one to pull N rows/objects per call, which results in clunky local code - ie an outer loop for grabbing a batch of data from the API and an inner loop for processing the elements of each batch.
I would prefer to push the outer loop into an iterator (that pulls down blocks of data and keeps track of the index into the external API etc) so I can write code more like this:
$datastream = new ApiIterator(/*params here*/);
foreach($datastream as $row){
// do inner loop processing here
}
Thanks!
从PHP 5.0开始,您可能需要使用PHP的Class Iterator接口( http://php.net/manual/en/class.iterator.php )并在ApiIterator Class中对其进行扩展(实现)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.