Linux 拨号vps windows公众号手机端

php对象转数组怎么转

lewis 6年前 (2019-03-11) 阅读数 11 #程序编程
文章标签 PHPphp对象转数组

在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
)
版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门