在 PHP 中,instanceof
关键字主要用于检查对象是否属于某个类或接口类型。对于数组,这个操作是不适用的。如果你想要检查一个变量是否是数组,你可以使用 is_array()
函数。例如:
$array = array(1, 2, 3);
$result = is_array($array);
if ($result) {
echo "The given variable is an array.";
} else {
echo "The given variable is not an array.";
}
如果你想要检查数组中的元素是否是某个类或接口类型,你可以遍历数组并逐个使用 instanceof
关键字。例如:
class MyClass {}
$array = array(new MyClass(), new MyClass());
foreach ($array as $element) {
if ($element instanceof MyClass) {
echo "The element is an instance of MyClass.";
} else {
echo "The element is not an instance of MyClass.";
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>