PHP对象如何转换为数组?
时间:2024-12-01
浏览:359
作者:佚名
珍惜时间,勤奋学习!今天给大家带来《PHP对象如何转换为数组?》,正文内容主要涉及到等等,如果你正在学习文章,或者是对文章有疑问,欢迎大家关注我!后面我会持续更新相关内容的,希望都能帮到正在学习的大家!

如何将 php 对象转换为数组?
在 php 中,如果获取到类似以下数据库数据:
object(think\collection)#117 (1) {
["items":protected] => array(10) {
[0] => array(5) {
["id"] => int(2)
["post_id"] => int(7)
["category_id"] => int(9)
["list_order"] => float(10000)
["status"] => int(1)
}
// ... 还有其他 9 个数组元素
}
}并希望将其转换为数组怎么办?
解决方案:
遇到这种情况时,可以调用 toarray 方法:
$data->toarray();
toarray 方法会将集合对象转换为标准 php 数组。
示例:
$data = object(think\collection)#117 (1) {
["items":protected] => array(10) {
// ... 内容与上面相同
}
};
var_dump($data->toarray());输出结果:
array(10) {
[0] => array(5) {
["id"] => int(2)
["post_id"] => int(7)
["category_id"] => int(9)
["list_order"] => float(10000)
["status"] => int(1)
}
// ... 还有其他 9 个数组元素
}今天关于《PHP对象如何转换为数组?》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.20
11.20
11.20
11.20
热门专题
