How to check if a variable is an array? ... or something like an array

I want to use a foreach with a variable, but this variable can be of many different types, for example NULL .

So, before foreach I am testing it:

 if(is_array($var)){ foreach($var as ... 

But I realized that it could also be a class that implements the Iterator interface. Maybe I'm blind, but how to check if a class implements an interface? Is there something like the is_a function or the inherits operator? I found class_implements , I can use it, but maybe there is something simpler?

And secondly, more importantly, I believe that this function exists, would it be enough to check if the variable is_array or "implements the Iterator interface" or do I need to check something else?

+76
arrays php foreach
Mar 24 '13 at 21:00
source share
6 answers

If you use foreach inside a function and you expect an array or Traversable object, you can enter a hint that the function is with:

 function myFunction(array $a) function myFunction(Traversable) 

If you are not using foreach inside a function, or you expect how you can simply use this construct to check if you can iterate over a variable:

 if (is_array($a) or ($a instanceof Traversable)) 
+59
Mar 24 '13 at 9:11
source share
β€” -

foreach can handle arrays and objects. You can check this with:

 $can_foreach = is_array($var) || is_object($var); if ($can_foreach) { foreach ($var as ... } 

You don't need to specifically check Traversable as others allude to this in their answers, because all objects - like all arrays - are passable in PHP.

More technically:

foreach works with all kinds of traversables, that is, with arrays, with simple objects (where get_iterator available properties) and Traversable objects (or rather, objects that define the internal get_iterator handler).

( source )

Simply put, in normal PHP programming, when a variable

  • an array
  • an object

and not

  • ZERO
  • resource
  • scalar

You can use foreach on it.

+10
May 25 '15 at 10:03
source share

You can check the Traversable instance with a simple function. This will work for all this Iterator , because Iterator extends Traversable

 function canLoop($mixed) { return is_array($mixed) || $mixed instanceof Traversable ? true : false; } 
+5
Mar 24 '13 at 21:09
source share
 <?php $var = new ArrayIterator(); var_dump(is_array($var), ($var instanceof ArrayIterator)); 

returns bool(false) or bool(true)

+2
Mar 24 '13 at 21:04
source share

the functions

 <?php /** * Is Array? * @param mixed $x * @return bool */ function isArray($x) : bool { return !isAssociative($x); } /** * Is Associative Array? * @param mixed $x * @return bool */ function isAssociative($x) : bool { if (!is_array($array)) { return false; } $i = count($array); while ($i > 0) { if (!isset($array[--$i])) { return true; } } return false; } 

example

 <?php $arr = [ 'foo', 'bar' ]; $obj = [ 'foo' => 'bar' ]; var_dump(isAssociative($arr)); # bool(false) var_dump(isAssociative($obj)); # bool(true) var_dump(isArray($obj)); # bool(false) var_dump(isArray($arr)); # bool(true) 
0
Dec 18 '18 at 0:27
source share

Starting with PHP 7.1, iterable exists for this purpose. iterable accepts any array, as well as any implementation of the Traversable interface. PHP 7.1 also introduced the is_iterable() function. For older versions, see Other Answers here for performing equivalent enforcement without new built-in functions.

Fair play: as BlackHole pointed out, does this question seem to be a duplicate of Iterable objects and array type hints? and his or her answer becomes more detailed than mine.

0
Dec 18 '18 at 0:45
source share



All Articles