PHP foreach:将每个循环结果放入变量
我试图将PHP foreach:将每个循环结果放入变量,php,variables,object,foreach,store,Php,Variables,Object,Foreach,Store,我试图将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, f
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_列将获取数组键中键为“weld”的所有值。“内爆”将生成一个数组字符串,值之间带有逗号。如果有外部循环,则显示该alsoupdate total code的代码$string在哪里启动?此代码没有意义。仅用于向数组中添加逗号。您可以从该初始数组中提取值,然后将它们连接在一起。如果这样做,结果的末尾将有一个逗号。为什么不把焊缝收集成一个阵列,然后内爆呢?