在PHP中,可以使用get_object_vars()
函數將對象轉換為數組。下面是一個示例:
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 25;
$array = get_object_vars($person);
print_r($array);
輸出結果為:
Array
(
[name] => John
[age] => 25
)
另外,你也可以使用json_decode()
函數將對象轉換為數組。下面是一個示例:
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 25;
$json = json_encode($person);
$array = json_decode($json, true);
print_r($array);
輸出結果為:
Array
(
[name] => John
[age] => 25
)