php 在laravel中将对象转换为数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28658891/
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-08-26 00:00:21  来源:igfitidea点击:

Converting object to array in laravel

phparraysobjectlaraveltype-conversion

提问by 8yt3c0d3

i queried a DB like this which got me an array:

我查询了这样的数据库,它给了我一个数组:

foreach($oid as $orderid) {
    $orderdetailData[] = DB::table('order_details')
        ->join('orders', 'order_details.oid', '=', 'orders.oid')
        ->select('order_details.oid', 'orders.ostatus')
        ->where('order_details.oid', $orderid)->get();
    }

    $data = array_flatten($orderdetailData);
    return $data;

This is the array i get

这是我得到的数组

array (size=2)
  0 => 
    object(stdClass)[174]
      public 'oid' => int 1
      public 'ostatus' => string 'Placed' (length=6)
  1 => 
    object(stdClass)[158]
      public 'oid' => int 2
      public 'ostatus' => string 'Placed' (length=6)

I am trying to get this array in the form

我试图以这种形式获取这个数组

array (size=2)
  0 => 
    array (size=2)
      public 'oid' => int 1
      public 'ostatus' => string 'Placed' (length=6)
  1 => 
    array (size=2)
      public 'oid' => int 2
      public 'ostatus' => string 'Placed' (length=6)

I tried doing this:

我尝试这样做:

foreach($orderdetailData as $key => $value){
    $data[] = array_flatten($orderdetailData[$key]);
}

But doing this gets me an array in this form:

但是这样做会让我得到一个这种形式的数组:

array (size=2)
  0 => 
    array (size=1)
      0 => 
        object(stdClass)[174]
          public 'oid' => int 1
          public 'ostatus' => string 'Placed' (length=6)
  1 => 
    array (size=1)
      0 => 
        object(stdClass)[158]
          public 'oid' => int 2
          public 'ostatus' => string 'Placed' (length=6)

Which is not i what i am looking for. Can someone tell me what would be an easy way to do this ? Thanks

这不是我要找的。有人可以告诉我有什么简单的方法可以做到这一点吗?谢谢

回答by lukasgeiter

Using array_mapand casting to an array should be sufficient:

使用array_map并转换为数组就足够了:

$data = array_map(function($object){
    return (array) $object;
}, $data);

I also wouldn't run queries inside a loop. You should try getting that data in one query from the db. Something like this could work:

我也不会在循环内运行查询。您应该尝试从数据库中的一个查询中获取该数据。这样的事情可以工作:

$data = DB::table('order_details')
    ->join('orders', 'order_details.oid', '=', 'orders.oid')
    ->select('order_details.oid', 'orders.ostatus')
    ->whereIn('order_details.oid', $oid)->get();

Edit

编辑

As it has been mentioned in another answer shortly let me explain how you can accomplish the same by setting PDO to FETCH_ASSOC:

正如在另一个答案中提到的,让我解释一下如何通过将 PDO 设置为FETCH_ASSOC

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();

However this changes the fetch mode globally for the rest of the request (at least if you don't open a new connection). To be save you should change it back afterwards:

但是,这会为请求的其余部分全局更改获取模式(至少在您不打开新连接的情况下)。为了保存,您应该在之后将其改回:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);

Or even back it up first if you can't be sure what default is used:

或者,如果您不能确定使用的是什么默认值,甚至可以先备份它:

$fetchModeBefore = DB::getFetchMode();
DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode($fetchModeBefore);

回答by Bradley Weston

On the PDO/DB object you can set the return style to assoc.

在 PDO/DB 对象上,您可以将返回样式设置为 assoc。