Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking with is_array for Arrays and ArrayObjects

Tags:

php

I have some legacy code and refactored an array to an ArrayObject. Now I have problems checking, if a variable is an array:

 if (is_array($entity) && $otherCondition) {
      // do something
 }

The function is_array() returns false on an ArrayObject. See this report.

Simplest solution would be to use something like this:

 function is_traversable($var) {
     return is_array($var) || $var instanceof Traversable;
 }

Is there some native way for PHP to do a check like this?

like image 906
Trendfischer Avatar asked Jan 01 '26 02:01

Trendfischer


1 Answers

according to http://blog.stuartherbert.com/php/2010/07/19/should-is_array-accept-arrayobject/, you have to make the custom method you wrote in order to do what you wish...

like image 94
Random Avatar answered Jan 03 '26 16:01

Random



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!