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

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

问题描述

我的数组类似于

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 pure multi-dimentional array?

推荐答案

你有没有尝试过的类型转换

have you tried typecasting

$array = (array) $object;

另外还有一个绝招其实

there is another trick actually

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

请参阅 json_en code PHP手册,第二个参数被称为的 assoc命令的:

assoc命令

TRUE ,返回的对象将转换成关联数组。

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

这是你在寻找什么。

您可能想试试这个,太:转换对象要阵列PHP (phpro.org)

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

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

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