Hướng dẫn is_iterable in php

(PHP 7 >= 7.1.0, PHP 8)

is_iterable Verify that the contents of a variable is an iterable value

Description

is_iterable(mixed $value): bool

Parameters

value

The value to check

Return Values

Returns true if value is iterable, false otherwise.

Examples

Example #1 is_iterable() examples

<?php

var_dump

(is_iterable([123]));  // bool(true)
var_dump(is_iterable(new ArrayIterator([123])));  // bool(true)
var_dump(is_iterable((function () { yield 1; })()));  // bool(true)
var_dump(is_iterable(1));  // bool(false)
var_dump(is_iterable(new stdClass()));  // bool(false)?>

See Also

  • is_array() - Finds whether a variable is an array

mopsyd at me dot com

4 years ago

A slight correction to brcontainer's polyfill, which prevents errors on a non-object in a non-blocking way, and also corrects the issue of  the conditional checking "file_exists" instead of the correct "function_exists":

if ( !function_exists(  'is_iterable' ) )
{

    function is_iterable( $obj )
    {
        return is_array( $obj ) || ( is_object( $obj ) && ( $obj instanceof \Traversable ) );
    }

}

The original answer would not have resolved correctly, because it was looking for a file instead of a function, and the provided method would error if given a non-iterable non-object value such as false.

brcontainer at yahoo dot com dot br

4 years ago

Polyfill for PHP5.6 and PHP7.0

    if (!file_exists('is_iterable')) {
        function is_iterable($obj)
        {
            return is_array($obj) || $obj instanceof \Traversable;
        }
    }

❮ PHP Variable Handling Reference

Example

Check whether the contents of a variable is an iterable value or not:

<?php
$a = "Hello";
echo "a is " . is_iterable($a) . "<br>";

$b = array("red", "green", "blue");
echo "b is " . is_iterable($b) . "<br>";

$c = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "c is " . is_iterable($c) . "<br>";

$d = [1, 2, 3];
echo "d is " . is_iterable($d) . "<br>";
?>

Try it Yourself »


Definition and Usage

The is_iterable() function checks whether the contents of a variable is an iterable value or not.

This function returns true (1) if the variable is iterable, otherwise it returns false/nothing.


Syntax

Parameter Values

ParameterDescription
variable Required. Specifies the variable to check

Technical Details

Return Value:TRUE if variable is iterable, FALSE otherwise
Return Type:Boolean
PHP Version:7.1+

❮ PHP Variable Handling Reference


View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The is_iterable() function is an inbuilt function in PHP which is used to check whether the contents of a variable is an iterable value or not.

    Syntax:

    bool is_iterable( mixed $var )

    Parameters: This function accepts single parameter as mentioned above and described below:

    • $var: It contains the value of variable that need to be check.

    Return Value: It returns TRUE if the value of variable is iterable, FALSE otherwise.

    Program 1:

    <?php

    $arr = array(1, 2, 3, 4, 5);

    if(is_iterable($arr)) {

        echo "Array is iterable";

    }

    else {

        echo "Array is not iterable";

    }

    class GFG {

    }

    $obj = new GFG();

    if(is_iterable($obj)) {

        echo "\nObject is iterable";

    }

    else {

        echo "\nObject is not iterable";

    }

    ?>

    Output:

    Array is iterable
    Object is not iterable

    Program 2:

    <?php

    class GFG {

        public $Geek_name = "Welcome to GeeksforGeeks"

    }

    $obj = new GFG();

    var_dump(is_iterable($obj));

    $arr = array('G', 'e', 'e', 'k', 's');

    var_dump(is_iterable($arr));

    $num = 25;

    var_dump(is_iterable($num));

    $str = "GeeksforGeeks";

    var_dump(is_iterable($str));

    $bool = true;

    var_dump(is_iterable($bool));

    ?>

    Output:

    bool(false) 
    bool(true) 
    bool(false) 
    bool(false) 
    bool(false) 

    Reference: https://www.php.net/manual/en/function.is-iterable.php