PHP foreach: 将每个循环结果放入变量中

PHP foreach: put each of the loop result in variable

我正在尝试将 foreach 中的值存储到一个变量中。但我似乎无法让它工作:

    foreach ($chunks as $key)   {
      // Get the Excel template path
      $path = storage_path('/app/excel/331-3-17 72AN - 21.3 - Ст. 20.xlsx');

      // Load the excel template file
      Excel::load($path, function($reader) use (&$key) {

      // Load the Excel sheet
      $objExcel = $reader->getExcel();
      $sheet = $objExcel->getSheet(0);

        foreach ($key as $row) {

           $welds = $row->weld;

           $string .= $welds.',';

      }

    $sheet->getCell('B25')->setValue($string);

    })
    // Set filename & store it
    ->setFilename("331-3-17 72AN - 21.3 - Ст. 20 " . date("d.m.y").'-'.mt_rand())
    ->store('xlsx', storage_path('/app/exports/21.3 - 2.5 - Ст20 '.date('m-d-Y')));
  }

我得到的结果是:

КСС1814.

一个字符串的预期结果是:

КСС1810, КСС1811, КСС1812, КСС1813, КСС1814

不确定这是否正确,但这是一个猜测。
使用数组列并用逗号内爆。

$welds = array_column($key, "weld");
$string = implode(",", $welds);

Array_column 将获取键为 "weld".
的数组键中的所有值 Iplode 将在值之间用逗号构建一个数组字符串。