【问题标题】:laravel manipulation array without for loop for better performance and optimizationlaravel 操作数组没有 for 循环以获得更好的性能和优化
【发布时间】:2019-03-20 03:19:10
【问题描述】:

我有一个类似的 JSON:

[
  {
    "id": 1,
    "order_id": 1,
    "product_id": 2,
    "quantity": "15.00",
    "created_at": "2019-03-19 10:02:40",
    "products": [
      {
        "id": 2,
        "name": "24 mantra",
        "sale_price": "45.00"
      }
    ]
  },
  {
    "id": 3,
    "order_id": 2,
    "product_id": 2,
    "quantity": "15.00",
    "created_at": "2019-03-19 10:16:15",
    "products": [
      {
        "id": 2,
        "name": "24 mantra",
        "sale_price": "45.00"
      }
    ]
  },
  {
    "id": 5,
    "order_id": 3,
    "product_id": 2,
    "quantity": "15.00",
    "created_at": "2019-03-19 10:16:19",
    "products": [
      {
        "id": 2,
        "name": "24 mantra",
        "sale_price": "45.00"
      }
    ]
  },
  {
    "id": 2,
    "order_id": 1,
    "product_id": 3,
    "quantity": "3.00",
    "created_at": "2019-03-19 10:02:40",
    "products": [
      {
        "id": 3,
        "name": "24 Mantra Jowar Atta 2LB",
        "sale_price": "45.00"
      }
    ]
  },
  {
    "id": 4,
    "order_id": 2,
    "product_id": 3,
    "quantity": "3.00",
    "created_at": "2019-03-19 10:16:16",
    "products": [
      {
        "id": 3,
        "name": "24 Mantra Jowar Atta 2LB",
        "sale_price": "45.00"
      }
    ]
  }
]

现在我想将总销售额乘以每个对象的 sale_price 的乘积和每个产品的 quantity * sale_price 的总和。

预期输出

[
  {
   "product": "24 mantra",
   "sale": 2025
  },
  {
   "product": "24 Mantra Jowar Atta 2LB",
   "sale": 270
  }
 ]

输出代码

  $orderlines = OrderLine->with('products:id,name,sale_price')->select('id','order_id','product_id','quantity','created_at')->get();
  $orderlines = $orderlines->groupBy('product_id');
  $totalproductsale = [];

  foreach($orderlines as $key => $singleline) {
    $total_bysales = 0.00;
        foreach($singleline as $singleproduct) {
          $total_bysales += $singleproduct->quantity * $singleproduct->sale_price;
        }
   array_push($totalproductsale,array("product" => $singleline['0']->products['0']->name,"totalsale" => number_format($total_bysales, 2, '.', '')));
   $totalsale[$key] = number_format($total_bysales, 2, '.', '');
      }

我想将总销售额和产品名称放入一个对象中,为此我使用了foreach 循环,但这非常耗时,我有 100 000 个数据进入生产服务器。仅将 30k 数据加载到图表中需要 15 秒。如何减少迭代时间或使用任何其他替代方案?

【问题讨论】:

  • $orderlines = (...)->get 需要多长时间?它是一个 SQL 查询吗? API 调用?它有多慢/快?
  • 这是雄辩的模型查询......我想将数据发送到前端。我使用 Angular 作为前端
  • 好的,但是你怎么知道循环需要 15 秒而不是那个查询?
  • 我不知道哪个需要更长的时间,但是当从网络检查时,它显示 15 秒才能得到响应。
  • 东南亚以外的人会对什么是“lac 数据”感到困惑。所以请标准测量

标签: laravel loops optimization eloquent


【解决方案1】:

使用collect 帮助器转换集合中的数组并对其进行操作。有很多帮助函数可以实现你想要的,只需要几行代码。

【讨论】:

    【解决方案2】:

    你可以试试这个,你必须把json带到控制器。我直接粘贴了json

    $json = '[ { "id": 1, "order_id": 1, "product_id": 2, ... ]';
    $array = json_decode($json, true);
    $collection = collect($array);
    

    然后您可以使用集合轻松操作数据

    【讨论】:

      猜你喜欢
      • 2021-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多