I have a need to check if the elements in an array
are objects
or something else. So far I did it like this:
if((is_object($myArray[0]))) { ... }
However, on occasion situations dictate that the input array
does not have indexes that start with zero (or aren't even numeric
), therefore asking for $myArray[0]
will generate a Notice
, but will also return the wrong result in my condition if the first array element actually is an object
(but under another index).
The only way I can think of doing here is a foreach
loop where I would break out of it right on the first go.
foreach($myArray as $element) {
$areObjects = (is_object($element));
break;
}
if(($areObjects)) { ... }
But I am wondering if there is a faster code than this, because a foreach
loop seems unnecessary here.