将数组和包含对象的数组转换为纯数组

21 浏览
0 Comments

将数组和包含对象的数组转换为纯数组

这个问题已经有答案了

将PHP对象转换为关联数组

我的数组是这样的:

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [name] => demo1
        )
    [1] => stdClass Object
        (
            [id] => 2
            [name] => demo2
        )
    [2] => stdClass Object
        (
            [id] => 6
            [name] => otherdemo
        )
)

如何将整个数组(包括对象)转换为纯多维数组?

admin 更改状态以发布 2023年5月22日
0
0 Comments

只需使用:

json_decode(json_encode($yourArray), true);

0
0 Comments

你试过类型转换吗?

$array = (array) $object;

实际上还有另一个技巧

$json  = json_encode($object);
$array = json_decode($json, true);

你可以在PHP手册的json_decode里找到更多信息,第二个参数被称为assoc:

assoc

当为TRUE时,返回的对象将被转换为关联数组。

这正是你正在寻找的。

你也可以尝试一下这个:使用PHP将对象转换为数组 (phpro.org)

0