将数组和数组中的对象转换为纯数组 [英] Converting array and objects in array to pure array

查看:27
本文介绍了将数组和数组中的对象转换为纯数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数组就像:

数组([0] =>标准类对象([id] =>1[名称] =>演示1)[1] =>标准类对象([id] =>2[名称] =>演示2)[2] =>标准类对象([id] =>6[名称] =>其他演示))

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

解决方案

您是否尝试过类型转换?

$array = (array) $object;

其实还有一个技巧

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

您可以在此处获得更多信息json_decode在PHP手册中,第二个参数叫做assoc:

<块引用>

关联

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

这正是您要找的.

您可能也想试试这个:Convert使用 PHP 对象到数组 (phpro.org)

My array is like:

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

How can I convert the whole array (including objects) to a pure multi-dimensional array?

解决方案

Have you tried typecasting?

$array = (array) $object;

There is another trick actually

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

You can have more info here json_decode in the PHP manual, the second parameter is called assoc:

assoc

When TRUE, returned objects will be converted into associative arrays.

Which is exactly what you're looking for.

You may want to try this, too : Convert Object To Array With PHP (phpro.org)

这篇关于将数组和数组中的对象转换为纯数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆