php 将 Laravel 对象转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26174267/
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
Convert laravel object to array
提问by Your Friend
Laravel output:
Laravel 输出:
Array
(
[0] = stdClass Object
(
[ID] = 5
)
[1] = stdClass Object
(
[ID] = 4
)
)
I want to convert this into normal array. Just want to remove that stdClass Object
. I also tried using ->toArray();
but I get an error:
我想将其转换为普通数组。只是想删除那个stdClass Object
。我也尝试使用,->toArray();
但出现错误:
Call to a member function toArray() on a non-object.
在非对象上调用成员函数 toArray()。
How can I fix this?
我怎样才能解决这个问题?
Functionalities have been Implemented on http://www.srihost.com
功能已在http://www.srihost.com 上实现
回答by Jarek Tkaczyk
UPDATE since version 5.4 of Laravel it is no longer possible.
更新自 Laravel 5.4 版以来不再可能。
You can change your db config, like @Varun suggested, or if you want to do it just in this very case, then:
您可以像@Varun 建议的那样更改您的数据库配置,或者如果您只想在这种情况下这样做,则:
DB::setFetchMode(PDO::FETCH_ASSOC);
// then
DB::table(..)->get(); // array of arrays instead of objects
// of course to revert the fetch mode you need to set it again
DB::setFetchMode(PDO::FETCH_CLASS);
For New Laravel above 5.4 (Ver > 5.4) see https://laravel.com/docs/5.4/upgradefetch mode section
对于高于 5.4 (Ver > 5.4) 的新 Laravel,请参阅https://laravel.com/docs/5.4/upgradefetch mode 部分
Event::listen(StatementPrepared::class, function ($event) {
$event->statement->setFetchMode(...);
});
回答by Maurice
foreach($yourArrayName as $object)
{
$arrays[] = $object->toArray();
}
// Dump array with object-arrays
dd($arrays);
Or when toArray()
fails because it's a stdClass
或者当toArray()
因为它是一个 stdClass 而失败时
foreach($yourArrayName as $object)
{
$arrays[] = (array) $object;
}
// Dump array with object-arrays
dd($arrays);
Not working? Maybe you can find your answer here:
不工作?也许你可以在这里找到你的答案:
回答by Touhid
this worked for me:
这对我有用:
$data=DB::table('table_name')->select(.......)->get();
$data=array_map(function($item){
return (array) $item;
},$data);
or
或者
$data=array_map(function($item){
return (array) $item;
},DB::table('table_name')->select(.......)->get());
回答by Varun Varunesh
You can also get all the result always as array by changing
您还可以通过更改始终以数组形式获取所有结果
// application/config/database.php
'fetch' => PDO::FETCH_CLASS,
// to
'fetch' => PDO::FETCH_ASSOC,
Hope this will help.
希望这会有所帮助。
回答by Selay
Just in case somebody still lands here looking for an answer. It can be done using plain PHP. An easier way is to reverse-json the object.
以防万一有人仍然在这里寻找答案。它可以使用普通的 PHP 来完成。一种更简单的方法是反向 json 对象。
function objectToArray(&$object)
{
return @json_decode(json_encode($object), true);
}
回答by scandar
this worked for me in laravel 5.4
这在 Laravel 5.4 中对我有用
$partnerProfileIds = DB::table('partner_profile_extras')->get()->pluck('partner_profile_id');
$partnerProfileIdsArray = $partnerProfileIds->all();
output
输出
array:4 [▼
0 => "8219c678-2d3e-11e8-a4a3-648099380678"
1 => "28459dcb-2d3f-11e8-a4a3-648099380678"
2 => "d5190f8e-2c31-11e8-8802-648099380678"
3 => "6d2845b6-2d3e-11e8-a4a3-648099380678"
]
回答by Marcin Nabia?ek
You need to iterate over the array
您需要遍历数组
for ($i = 0, $c = count($array); $i < $c; ++$i) {
$array[$i] = (array) $array[$i];
}
ans use (array)
conversion because you have array of objects of Std class and not object itself
ans 使用(array)
转换,因为您有 Std 类的对象数组而不是对象本身
Example:
例子:
$users = DB::table('users')->get();
var_dump($users);
echo "<br /><br />";
for ($i = 0, $c = count($users); $i < $c; ++$i) {
$users[$i] = (array) $users[$i];
}
var_dump($users);
exit;
Output for this is:
输出为:
array(1) { [0]=> object(stdClass)#258 (8) { ["id"]=> int(1) ["user_name"]=> string(5) "admin" ["email"]=> string(11) "admin@admin" ["passwd"]=> string(60) "y$T/0fW18gPGgz0CILTy2hguxNpcNjYZHsTyf5dvpor9lYMw/mtKYfi" ["balance"]=> string(4) "0.00" ["remember_token"]=> string(60) "moouXQOJFhtxkdl9ClEXYh9ioBSsRp28WZZbLPkJskcCr0325TyrxDK4al5H" ["created_at"]=> string(19) "2014-10-01 12:00:00" ["updated_at"]=> string(19) "2014-09-27 12:20:54" } }
array(1) { [0]=> array(8) { ["id"]=> int(1) ["user_name"]=> string(5) "admin" ["email"]=> string(11) "admin@admin" ["passwd"]=> string(60) "y$T/0fW18gPGgz0CILTy2hguxNpcNjYZHsTyf5dvpor9lYMw/mtKYfi" ["balance"]=> string(4) "0.00" ["remember_token"]=> string(60) "moouXQOJFhtxkdl9ClEXYh9ioBSsRp28WZZbLPkJskcCr0325TyrxDK4al5H" ["created_at"]=> string(19) "2014-10-01 12:00:00" ["updated_at"]=> string(19) "2014-09-27 12:20:54" } }
as expected. Object of stdClass
has been converted to array.
正如预期的那样。的对象stdClass
已转换为数组。
回答by Ariel Ruiz
If you want to get only ID in array, can use array_map:
如果只想获取数组中的 ID,可以使用 array_map:
$data = array_map(function($object){
return $object->ID;
}, $data);
With that, return an array with ID in every pos.
这样,在每个位置返回一个带有 ID 的数组。
回答by kunal
Its very simple. You can use like this :-
它非常简单。你可以这样使用:-
Suppose You have one users table and you want to fetch the id only
$users = DB::table('users')->select('id')->get();
$users = json_decode(json_encode($users)); //it will return you stdclass object
$users = json_decode(json_encode($users),true); //it will return you data in array
echo '<pre>'; print_r($users);
Hope it helps
希望能帮助到你
回答by NinoMarconi
It's also possible to typecast an object to an array. That worked for me here.
也可以将对象类型转换为数组。这对我有用。
(array) $object;
(array) $object;
will convert
会转换
stdClass Object
(
[id] => 4
)
to
到
Array(
[id] => 4
)
Had the same problem when trying to pass data from query builder to a view. Since data comes as object. So you can do:
尝试将数据从查询构建器传递到视图时遇到了同样的问题。由于数据是作为对象出现的。所以你可以这样做:
$view = view('template', (array) $object);
And in your view you use variables like
在您看来,您使用的变量是
{{ $id }}