• <i id='ZObgU'><tr id='ZObgU'><dt id='ZObgU'><q id='ZObgU'><span id='ZObgU'><b id='ZObgU'><form id='ZObgU'><ins id='ZObgU'></ins><ul id='ZObgU'></ul><sub id='ZObgU'></sub></form><legend id='ZObgU'></legend><bdo id='ZObgU'><pre id='ZObgU'><center id='ZObgU'></center></pre></bdo></b><th id='ZObgU'></th></span></q></dt></tr></i><div id='ZObgU'><tfoot id='ZObgU'></tfoot><dl id='ZObgU'><fieldset id='ZObgU'></fieldset></dl></div>
    1. <small id='ZObgU'></small><noframes id='ZObgU'>

        • <bdo id='ZObgU'></bdo><ul id='ZObgU'></ul>

        <tfoot id='ZObgU'></tfoot>
      1. <legend id='ZObgU'><style id='ZObgU'><dir id='ZObgU'><q id='ZObgU'></q></dir></style></legend>

        在laravel中将对象转换为数组

        Converting object to array in laravel(在laravel中将对象转换为数组)
      2. <small id='ZQO4i'></small><noframes id='ZQO4i'>

        • <bdo id='ZQO4i'></bdo><ul id='ZQO4i'></ul>
          <legend id='ZQO4i'><style id='ZQO4i'><dir id='ZQO4i'><q id='ZQO4i'></q></dir></style></legend>
          <tfoot id='ZQO4i'></tfoot>

                  <tbody id='ZQO4i'></tbody>
                <i id='ZQO4i'><tr id='ZQO4i'><dt id='ZQO4i'><q id='ZQO4i'><span id='ZQO4i'><b id='ZQO4i'><form id='ZQO4i'><ins id='ZQO4i'></ins><ul id='ZQO4i'></ul><sub id='ZQO4i'></sub></form><legend id='ZQO4i'></legend><bdo id='ZQO4i'><pre id='ZQO4i'><center id='ZQO4i'></center></pre></bdo></b><th id='ZQO4i'></th></span></q></dt></tr></i><div id='ZQO4i'><tfoot id='ZQO4i'></tfoot><dl id='ZQO4i'><fieldset id='ZQO4i'></fieldset></dl></div>
                  本文介绍了在laravel中将对象转换为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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;
                  

                  这是我得到的数组

                  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)
                  

                  我试过这样做:

                  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

                  推荐答案

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

                  Using array_map and casting to an array should be sufficient:

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

                  编辑

                  正如在另一个答案中提到的那样,让我解释一下如何通过将 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);
                  

                  这篇关于在laravel中将对象转换为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Converting string to MySQL timestamp format in php(在php中将字符串转换为MySQL时间戳格式)
                  datetime to timestamp(日期时间到时间戳)
                  PHP timestamp date to user timezone(PHP时间戳日期到用户时区)
                  Converting TIMESTAMP to unix time in PHP?(在 PHP 中将 TIMESTAMP 转换为 unix 时间?)
                  Convert ISO 8601 to unixtimestamp(将 ISO 8601 转换为 unixtimestamp)
                  Finding days between 2 unix timestamps in php(在php中查找2个unix时间戳之间的天数)
                1. <small id='wVmiM'></small><noframes id='wVmiM'>

                  <i id='wVmiM'><tr id='wVmiM'><dt id='wVmiM'><q id='wVmiM'><span id='wVmiM'><b id='wVmiM'><form id='wVmiM'><ins id='wVmiM'></ins><ul id='wVmiM'></ul><sub id='wVmiM'></sub></form><legend id='wVmiM'></legend><bdo id='wVmiM'><pre id='wVmiM'><center id='wVmiM'></center></pre></bdo></b><th id='wVmiM'></th></span></q></dt></tr></i><div id='wVmiM'><tfoot id='wVmiM'></tfoot><dl id='wVmiM'><fieldset id='wVmiM'></fieldset></dl></div>
                      • <bdo id='wVmiM'></bdo><ul id='wVmiM'></ul>
                        <tfoot id='wVmiM'></tfoot>
                        <legend id='wVmiM'><style id='wVmiM'><dir id='wVmiM'><q id='wVmiM'></q></dir></style></legend>

                              <tbody id='wVmiM'></tbody>