Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在laravel中将对象转换为数组_Php_Arrays_Object_Laravel_Type Conversion - Fatal编程技术网

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

Php 在laravel中将对象转换为数组,php,arrays,object,laravel,type-conversion,Php,Arrays,Object,Laravel,Type Conversion,我查询了这样一个数据库,得到了一个数组: foreach($oid as $orderid) { $orderdetailData[] = DB::table('order_details') ->join('orders', 'order_details.oid', '=', 'orders.oid') ->select('order_details.oid', 'orders.ostatus') ->where('or

我查询了这样一个数据库,得到了一个数组:

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;
这是我得到的数组

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)
我正在尝试将此数组设置为

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)
我试着这样做:

foreach($orderdetailData as $key => $value){
    $data[] = array_flatten($orderdetailData[$key]);
}
但这样做会得到一个如下形式的数组:

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)

这不是我要找的。有人能告诉我做这件事的简单方法是什么吗?感谢使用
数组映射并对数组进行强制转换就足够了:

$data = array_map(function($object){
    return (array) $object;
}, $data);
我也不会在循环中运行查询。您应该尝试从数据库中的一个查询中获取该数据。类似这样的方法可能会奏效:

$data = DB::table('order_details')
    ->join('orders', 'order_details.oid', '=', 'orders.oid')
    ->select('order_details.oid', 'orders.ostatus')
    ->whereIn('order_details.oid', $oid)->get();
编辑 正如在另一个简短的回答中提到的,让我解释一下如何通过将PDO设置为
FETCH_ASSOC
来实现同样的功能:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
但是,这会全局更改请求其余部分的获取模式(至少在不打开新连接的情况下)。要保存,您应在以后将其更改回:

DB::setFetchMode(PDO::FETCH_ASSOC);
$data = DB::table('order_details') .... ->get();
DB::setFetchMode(PDO::FETCH_CLASS);
或者,如果您无法确定使用的默认设置,甚至可以先备份:

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

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

您可以检查此问题的解决方案。它解释了如何将对象转换为数组。感谢您的回复,今天学到了一些新的东西:)当心由此带来的性能下降。我试图直接从对象获取值,但我遇到了“无法将MyClass类型的对象用作数组”错误。我如何直接从类获取值将立即尝试:)感谢您的回复