2017-11-30 7 views
-1

foreach의 값을 하나의 변수에 저장하려고합니다. 하지만 난 그것을 얻이 수없는 것 : 내가 얻을PHP 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 

enter image description here enter image description here

+0

외부 루프가있는 경우 해당 코드도 표시 –

+0

업데이트 총 코드 – kranthi

+0

$ string은 (는) 어디에서 시작 했습니까? – laiju

답변

2

이것이 맞는지는 모르겠지만 추측입니다.
배열 열을 사용하고 쉼표로 함입하십시오.

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

Array_column은 키가 "weld"인 배열 키의 모든 값을 가져옵니다.
Implode는 값 사이에 쉼표가있는 배열 문자열을 만듭니다.