mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-14 14:28:27 +08:00
Add helper function for checking if an object is an array-like object.
Added support for traversable objects in `BaseHtml` and `ArrayHelper`
This commit is contained in:

committed by
SilverFire - Dmitry Naumenko

parent
80e9331fe0
commit
16a6af5fa8
@ -680,7 +680,7 @@ class BaseArrayHelper
|
||||
* Check whether an array or [[\Traversable]] contains an element.
|
||||
*
|
||||
* This method does the same as the PHP function [in_array()](http://php.net/manual/en/function.in-array.php)
|
||||
* but it does not only work for arrays but also objects that implement the [[\Traversable]] interface.
|
||||
* but additionally works for objects that implement the [[\Traversable]] interface.
|
||||
* @param mixed $needle The value to look for.
|
||||
* @param array|\Traversable $haystack The set of values to search.
|
||||
* @param boolean $strict Whether to enable strict (`===`) comparison.
|
||||
@ -706,6 +706,21 @@ class BaseArrayHelper
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a variable is an array or [[\Traversable]].
|
||||
*
|
||||
* This method does the same as the PHP function [is_array()](http://php.net/manual/en/function.is-array.php)
|
||||
* but additionally works objects that implement the [[\Traversable]] interface.
|
||||
* @param mixed $var The variable being evaluated.
|
||||
* @return boolean whether $var is array-like
|
||||
* @see http://php.net/manual/en/function.is_array.php
|
||||
* @since 2.0.8
|
||||
*/
|
||||
public static function isTraversable($var)
|
||||
{
|
||||
return is_array($var) || $var instanceof \Traversable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether an array or [[\Traversable]] is a subset of another array or [[\Traversable]].
|
||||
*
|
||||
|
Reference in New Issue
Block a user