views:

43

answers:

2

Hello,

I am passing a JSON'd feed from Twitter into my application, and sometimes Twitters API returns empty or incomplete feeds. This is causing a problem when I try to iterate the feeds using foreach - does anyone know of a suitable test to find out if an object or array is iterable?

Could this be done with isarray($obj) || $obj instanceof Iterator or are there any other cases I should look out for?

Thanks

A: 

Extend the Iterator class to implement a counting method.

MyIterator extends Iterator {
  public MyIterator isIterator () {
    return count($this);
  }
}
Andrew Sledge
Whether an instance is iterable with `foreach` has nothing with to do with the `count()` method. Also, `Iterator` does not have a `count()` method. If your class should be countable, you can implement the `Countable` interface.
Gordon
Not to mention that the code you provide isn't even valid PHP for a number of reasons...
ircmaxell
Thanks, but I was asking how you test an object / arrays suitability for `foreach`, not how to implement an Iterator.
Kevin Sedgley
A: 

Answered here: http://stackoverflow.com/questions/3584700/iterable-objects-and-array-type-hinting/3584781#3584781

Credit to boltclock

Kevin Sedgley
You are not supposed to provide the link to a possible duplicate as an answer, especially if it's already linked as such in the comments below the question. You could have simply deleted your entire question.
Gordon