溫馨提示×

在PHP中is_array的用法實例

PHP
小樊
83
2024-09-12 11:40:34
欄目: 編程語言

在PHP中,is_array()函數(shù)用于檢查給定的值是否是一個數(shù)組

示例1:檢查字符串是否為數(shù)組

$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:檢查整數(shù)是否為數(shù)組

$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:檢查多維數(shù)組是否為數(shù)組

$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:檢查對象是否為數(shù)組

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.

0