phant0m Posted December 7, 2010 Share Posted December 7, 2010 How can I check, whether something can be iterated with for, without doing it the hard way with exceptions? Would I just do "is_array or instanceof Traversable"? Quote Link to comment https://forums.phpfreaks.com/topic/220901-check-whether-something-is-iterable-using-foreach/ Share on other sites More sharing options...
sweeb Posted December 7, 2010 Share Posted December 7, 2010 It sounds like you're asking how to tell whether an array is associative or not. There are plenty of examples on the php.net is_array page in the comments on how to do this http://php.net/manual/en/function.is-array.php Quote Link to comment https://forums.phpfreaks.com/topic/220901-check-whether-something-is-iterable-using-foreach/#findComment-1143893 Share on other sites More sharing options...
phant0m Posted December 7, 2010 Author Share Posted December 7, 2010 No, I want a simple way to check whether I can do foreach on something. That something could be an array, but also an object that implements the Traverseble interface, i.e. has an Iterator of some kind. Quote Link to comment https://forums.phpfreaks.com/topic/220901-check-whether-something-is-iterable-using-foreach/#findComment-1143894 Share on other sites More sharing options...
salathe Posted December 7, 2010 Share Posted December 7, 2010 Would I just do "is_array or instanceof Traversable"? Yep. Quote Link to comment https://forums.phpfreaks.com/topic/220901-check-whether-something-is-iterable-using-foreach/#findComment-1143895 Share on other sites More sharing options...
phant0m Posted December 7, 2010 Author Share Posted December 7, 2010 Ok, thanks a lot. Quote Link to comment https://forums.phpfreaks.com/topic/220901-check-whether-something-is-iterable-using-foreach/#findComment-1143898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.