如何在laravel中将数组值附加到集合中?

时间:2018-10-23 05:53:14

标签: arrays laravel-5 append

我有一个名为$ dataItemregistrationdetail的查询的集合:

     $dataItemregistrationdetail = DB::table('itemregistrationdetail')
                                    ->join('sections', 'itemregistrationdetail.sectionid', '=', 'sections.sectionid')
                                    ->join('categories', 'itemregistrationdetail.categoryid', '=', 'categories.categoryid')
                                    ->join('kategori_kursus', 'itemregistrationdetail.kategoriid', '=', 'kategori_kursus.kategoriid')
                                    ->join('devices', 'itemregistrationdetail.deviceid', '=', 'devices.deviceid')
                                    ->select('itemregistrationdetail.*', 'sections.sectionname', 'categories.categoryname', 'kategori_kursus.kategorikursus', 'devices.kursusname')
                                    ->where('itemregistrationdetail.itemregistrationid', $id)
                                    ->get();

此集合输出以下内容:

 Collection {#557 ▼
 #items: array:4 [▼
  0 => {#552 ▼
  +"ItemRegistrationDetailID": 64852
  +"ItemRegistrationID": 194
  +"SectionID": 1
  +"CategoryID": 1
  +"KategoriID": 1
  +"DeviceID": 1129
  +"BrandID": 0
  +"ModelID": 0
  +"SerialNo": "SEKOLAH MENENGAH"
  +"StatusID": 1
  +"Imei": "MAKTAB PERGURUAN"
  +"No_tel": ""
  +"Comments": ""
  +"daymula_kursus": 9
  +"monthmula_kursus": "May"
  +"yearmula_kursus": 2011
  +"daytamat_kursus": 28
  +"monthtamat_kursus": "July"
  +"yeartamat_kursus": 2011
  +"sectionname": "I91"
  +"categoryname": "GURU BESAR"
  +"kategorikursus": "KURSUS (UMUM)"
  +"kursusname": "KURSUS ASAS"
  }
   1 => {#549 ▶}
   2 => {#555 ▶}
   3 => {#558 ▶}
 ]
}

我还有另一个日期数组。数组的值是$ dataItemregistration的每个元素的日期。日期数组名为$ datatarikhMula:

 $datatarikhMula = DB::table('itemregistrationdetail')
                  ->select('itemregistrationdetail.daymula_kursus', 'itemregistrationdetail.monthmula_kursus', 'itemregistrationdetail.yearmula_kursus')
                  ->where('itemregistrationdetail.itemregistrationid', $id)
                  ->get();

我已将上述集合格式化为该数组:

    //**************** format the value of the date************************  
    $dates = json_decode($datatarikhMula, true); 

    $dataTarikhMula = array();

    foreach($dates as $date)
    {
        $dataTarikhMula[] = implode(' ', array_values($date));
    }

数组dataTarikhMula的输出为:

  array:4 [▼
  0 => "9 May 2011"
  1 => "24 0 2013"
  2 => "12 0 2013"
  3 => "7 April 2014"
  ]

我需要将每个日期附加到每个dataItemregistrationdetail行,以便在dataItemregistrationdetail中将包含名为date的额外属性,该属性包含dataTarikhMula数组中的值。 因此,它将输出如下:

   Collection {#557 ▼
 #items: array:4 [▼
  0 => {#552 ▼
  +"ItemRegistrationDetailID": 64852
  +"ItemRegistrationID": 194
  +"SectionID": 1
  +"CategoryID": 1
  +"KategoriID": 1
  +"DeviceID": 1129
  +"BrandID": 0
  +"ModelID": 0
  +"SerialNo": "SEKOLAH MENENGAH"
  +"StatusID": 1
  +"Imei": "MAKTAB PERGURUAN"
  +"No_tel": ""
  +"Comments": ""
  +"daymula_kursus": 9
  +"monthmula_kursus": "May"
  +"yearmula_kursus": 2011
  +"daytamat_kursus": 28
  +"monthtamat_kursus": "July"
  +"yeartamat_kursus": 2011
  +"sectionname": "I91"
  +"categoryname": "GURU BESAR"
  +"kategorikursus": "KURSUS (UMUM)"
  +"kursusname": "KURSUS ASAS"
  +"date": "9 May 2011"  <------------------added value
  }
   1 => {#549 ▶}
   2 => {#555 ▶}
   3 => {#558 ▶}
 ]
}

如何将值附加到每个$ dataItemregistrationdetail数组元素中?

我已将代码修改为您建议的代码:

$dataItemregistrationdetail->map(function ($detail) {
$detail['date'] = "{$detail->daymula_kursus} {$detail->monthmula_kursus} {$detail->yearmula_kursus}";
return $detail;

});

但是错误显示如下。

1 个答案:

答案 0 :(得分:1)

您可以为每个集合项添加新元素,如下所示:

$dataItemregistrationdetail->map(function ($detail, $key) use ($dataTarikhMula) {
    $detail->date = $dataTarikhMula[$key] ?? null;
    return $detail;
});

我不确定这是否是您的最佳方法。您已经具有集合中的所有日期数据,为什么需要第二个查询来构建日期数组。您可以改为:

$dataItemregistrationdetail->map(function ($detail) {
    $detail->date = "{$detail->daymula_kursus} {$detail->monthmula_kursus} {$detail->yearmula_kursus}";
    return $detail;
});

这样,您就不需要第二个数组。