在PHP中,可以使用get_object_vars()
函數(shù)將對(duì)象轉(zhuǎn)換為數(shù)組。下面是一個(gè)示例:
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 25;
$array = get_object_vars($person);
print_r($array);
輸出結(jié)果為:
Array
(
[name] => John
[age] => 25
)
另外,你也可以使用json_decode()
函數(shù)將對(duì)象轉(zhuǎn)換為數(shù)組。下面是一個(gè)示例:
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);
輸出結(jié)果為:
Array
(
[name] => John
[age] => 25
)