将laravel对象转换为数组

问题描述 投票:17回答:12

Laravel输出:

Array
(
    [0] = stdClass Object
    (
        [ID] = 5

    )

    [1] = stdClass Object
    (
        [ID] = 4

    )

)

我想将其转换为普通数组。只是想删除那个stdClass Object。我也尝试使用->toArray();,但我收到一个错误:

在非对象上调用成员函数toArray()。

我怎样才能解决这个问题?

功能已在http://www.srihost.com上实施

php arrays laravel laravel-4 laravel-3
12个回答
34
投票

从Laravel版本5.4开始更新它已不再可能。

您可以像@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);

0
投票

它非常简单。你可以像这样使用: -

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);

希望能帮助到你


0
投票

$ foo = Bar :: getBeers(); $ foo = $ foo-> toArray();


0
投票
$res = ActivityServer::query()->select('channel_id')->where(['id' => $id])->first()->attributesToArray();

我使用get(),它返回一个对象,我使用attributesToArray()将对象属性更改为数组。


22
投票
foreach($yourArrayName as $object)
{
    $arrays[] = $object->toArray();
}
// Dump array with object-arrays
dd($arrays);

或者当toArray()失败,因为它是stdClass

foreach($yourArrayName as $object)
{
    $arrays[] =  (array) $object;
}
// Dump array with object-arrays
dd($arrays);

不工作?也许你可以在这里找到答案:

Convert PHP object to associative array


7
投票

您还可以通过更改将所有结果始终作为数组获取

// application/config/database.php

'fetch' => PDO::FETCH_CLASS,
 // to
'fetch' => PDO::FETCH_ASSOC,

希望这会有所帮助。


5
投票

这对我有用:

$data=DB::table('table_name')->select(.......)->get();
$data=array_map(function($item){
    return (array) $item;
},$data);

要么

$data=array_map(function($item){
    return (array) $item;
},DB::table('table_name')->select(.......)->get());

2
投票

这在laravel 5.4中对我有用

$partnerProfileIds = DB::table('partner_profile_extras')->get()->pluck('partner_profile_id');
$partnerProfileIdsArray = $partnerProfileIds->all();

产量

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"
]

https://laravel.com/api/5.4/Illuminate/Support/Collection.html#method_all


1
投票

您需要迭代数组

for ($i = 0, $c = count($array); $i < $c; ++$i) {
    $array[$i] = (array) $array[$i];
}

ans使用(array)转换,因为你有Std类的对象数组而不是对象本身

例:

$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;

输出为:

array(1) { [0]=> object(stdClass)#258 (8) { ["id"]=> int(1) ["user_name"]=> string(5) "admin" ["email"]=> string(11) "admin@admin" ["passwd"]=> string(60) "$2y$10$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) "$2y$10$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" } } 

正如所料。 stdClass的对象已转换为数组。


1
投票

如果您只想获取数组中的ID,可以使用array_map:

    $data = array_map(function($object){
        return $object->ID;
    }, $data);

有了它,返回每个pos中带有ID的数组。


1
投票

以防有人仍然在这里寻找答案。它可以使用普通的PHP来完成。一种更简单的方法是反向json对象。

function objectToArray(&$object)
{
    return @json_decode(json_encode($object), true);
}

0
投票

我建议你只是在你的方法中这样做

public function MyAwesomeMethod($returnQueryAs = null)
{
    $tablename = 'YourAwesomeTable';

    if($returnQueryAs == 'array')
    {
        DB::connection()->setFetchMode(PDO::FETCH_ASSOC);
    }

    return DB::table($tablename)->get();
}

有了这一切你需要的是传递字符串'数组'作为你的论点和Voila!返回一个关联数组。

© www.soinside.com 2019 - 2024. All rights reserved.