Checking for iterable PHP content

As for PHP> = 7.1, you can determine whether a variable is iterable or not using is_iterable().

is there any alternative to this for PHP <= 7?

how can i accomplish this since im working on php 7.0?

+4
source share
1 answer

You just need to check if a given var has a type Traversableor an array of arrays. Everything else is not iterable.

if (!function_exists('is_iterable')) {
    function is_iterable($var)
    {
        return is_array($var) || $var instanceof \Traversable;
    }
}
+7
source

Source: https://habr.com/ru/post/1676147/


All Articles