Laravel - 如何将 Stdclass 对象转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49047683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:27:30  来源:igfitidea点击:

Laravel - How to convert Stdclass object to Array

laravel

提问by webdev12

I'm facing this problem when use database in Laravel. How can i convert that to Array the most simpletest?

我在 Laravel 中使用数据库时遇到了这个问题。我怎样才能把它转换成最简单的数组?

$data = DB::table('users')->get();

回答by Alexandr Biship

Please try like this

请尝试这样

$result = json_decode(json_encode($data, true));

回答by Alexey Mezenin

get()will return a collection. If you want to get an array of objects, use the toArray()method:

get()将返回一个集合。如果要获取对象数组,请使用以下toArray()方法:

$data->toArray();

If you want to convert every object to an array too, do this:

如果您也想将每个对象转换为数组,请执行以下操作:

$data->map(function($i) {
    return (array)$i;
})->toArray();