在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
)