在PHP中,is_array()
函數用于檢查給定的值是否是一個數組
示例1:檢查字符串是否為數組
$str = "Hello, World!";
if (is_array($str)) {
echo "The given value is an array.";
} else {
echo "The given value is not an array.";
}
// 輸出:The given value is not an array.
示例2:檢查整數是否為數組
$num = 42;
if (is_array($num)) {
echo "The given value is an array.";
} else {
echo "The given value is not an array.";
}
// 輸出:The given value is not an array.
示例3:檢查多維數組是否為數組
$multi_dim_array = array(
"name" => "John",
"age" => 30,
"city" => "New York"
);
if (is_array($multi_dim_array)) {
echo "The given value is an array.";
} else {
echo "The given value is not an array.";
}
// 輸出:The given value is an array.
示例4:檢查對象是否為數組
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 30;
if (is_array($person)) {
echo "The given value is an array.";
} else {
echo "The given value is not an array.";
}
// 輸出:The given value is not an array.