【问题标题】:How to add array inside array based on PHP condition?如何根据 PHP 条件在数组内添加数组?
【发布时间】:2021-11-29 09:50:18
【问题描述】:

我有一个来自以下的 laravel 集合:$unitinventory = DB::select('call wh_inventory_list(?)', array('Units'));

[
   {
      "unit_id":"UCL2100001",
      "rr_id":"RR2100001",
      "make":"FAW",
      "chassis_no":"LFWSRXRJ9M1E00004",
      "engine_no":"CA6DM2-42E5153558354",
      "body_type":"TRACTOR HEAD",
      "horse_power":"420HP",
      "cabin_type":"J6P E5",
      "numwheels":"6W",
      "unit_status":"Available",
      "unit_location":null
   },
   {
      "unit_id":"UCL2100002",
      "rr_id":"RR2100002",
      "make":"FAW",
      "chassis_no":"LFWSRXRJ4M1E00007",
      "engine_no":"CA6DM2-42E5153563283",
      "body_type":"TRACTOR HEAD",
      "horse_power":"420HP",
      "cabin_type":"J6P E5",
      "numwheels":"6W",
      "unit_status":"Available",
      "unit_location":null
   }
]

还有另一个收藏:$modifification = collect(DB::table('pd_jo_bodymodification')->get());

[
   {
      "row_id":2,
      "jo_id":"JO2100003",
      "jo_chassisno":"LFWSRXRJ4M1E00007",
      "jo_convertedbody":"1st conversion",
      "jo_mileage":"test",
      "jo_serviceadvisor":"test",
      "jo_servicerequest":"test",
      "jo_remarks":"test"
   },
   {
      "row_id":4,
      "jo_id":"JO2100004",
      "jo_chassisno":"LFWSRXRJ4M1E00007",
      "jo_convertedbody":"2nd conversion",
      "jo_mileage":"test",
      "jo_serviceadvisor":"test",
      "jo_servicerequest":"test",
      "jo_remarks":"test"
   },
   {
      "row_id":4,
      "jo_id":"JO2100004",
      "jo_chassisno":"LDFDRETFGGF000RE",
      "jo_convertedbody":"OTHER CONVERSION",
      "jo_mileage":"test",
      "jo_serviceadvisor":"test",
      "jo_servicerequest":"test",
      "jo_remarks":"test"
   }
]

我想知道 $unitinventory->chassis_no 是否有修改历史,所以我这样做了:

foreach ($unitinventory as $key => $value) {
      $tmpmodif["conversion"] = $modifification->where('jo_chassisno',$value->chassis_no);
}

返回以下内容:

{
   "conversion":[
      {
         "row_id":2,
         "jo_id":"JO2100003",
         "jo_chassisno":"LFWSRXRJ4M1E00007",
         "jo_convertedbody":"1st conversion",
         "jo_mileage":"test",
         "jo_serviceadvisor":"test",
         "jo_servicerequest":"test",
         "jo_remarks":"test"
      },
      {
         "row_id":4,
         "jo_id":"JO2100004",
         "jo_chassisno":"LFWSRXRJ4M1E00007",
         "jo_convertedbody":"2nd conversion",
         "jo_mileage":"test",
         "jo_serviceadvisor":"test",
         "jo_servicerequest":"test",
         "jo_remarks":"test"
      }
   ]
}

现在我想将转换历史添加到$unitinventory 集合中,如下所示:

$unitinventory = 
[
   {
      "unit_id":"UCL2100001",
      "rr_id":"RR2100001",
      "make":"FAW",
      "chassis_no":"LFWSRXRJ9M1E00004",
      "engine_no":"CA6DM2-42E5153558354",
      "body_type":"TRACTOR HEAD",
      "horse_power":"420HP",
      "cabin_type":"J6P E5",
      "numwheels":"6W",
      "unit_status":"Available",
      "unit_location":null,
      "conversion":[]
   },
   {
      "unit_id":"UCL2100002",
      "rr_id":"RR2100002",
      "make":"FAW",
      "chassis_no":"LFWSRXRJ4M1E00007",
      "engine_no":"CA6DM2-42E5153563283",
      "body_type":"TRACTOR HEAD",
      "horse_power":"420HP",
      "cabin_type":"J6P E5",
      "numwheels":"6W",
      "unit_status":"Available",
      "unit_location":null,
      "conversion":[
         {
            "row_id":2,
            "jo_id":"JO2100003",
            "jo_chassisno":"LFWSRXRJ4M1E00007",
            "jo_convertedbody":"1st conversion",
            "jo_mileage":"test",
            "jo_serviceadvisor":"test",
            "jo_servicerequest":"test",
            "jo_remarks":"test"
         },
         {
            "row_id":4,
            "jo_id":"JO2100004",
            "jo_chassisno":"LFWSRXRJ4M1E00007",
            "jo_convertedbody":"2nd conversion",
            "jo_mileage":"test",
            "jo_serviceadvisor":"test",
            "jo_servicerequest":"test",
            "jo_remarks":"test"
         }
      ]
   }
]

如何使它成为可能?谢谢!

【问题讨论】:

  • 有了 Eloquent 模型和关系,这已经是预构建的了。
  • 你的表叫什么,这可以是具有雄辩模型的求解器。

标签: php laravel collections


【解决方案1】:

我用这种方法解决了问题:

foreach ($tmpmodif as $key => $value) {
    $unitinventory[$key] = array_merge(  (array)$value,   (array)["conversion"=>$modifification->where('jo_chassisno',$value->chassis_no)]);
}

合并foreach内的数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-17
    • 2021-01-24
    • 2020-09-05
    • 2023-03-13
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多