2013-10-07 2 views
0

다음 코드를 사용하여 데이터의 배열 (쉼표로 구분되었지만 파일에서 분리됨)을 사용할 수있는 배열로 변경하고 있습니다. 다음과 같이 내 코드는 ... $ 포함한 FileContent의PHP 폭발 - 다차원 배열을 사용하는 다중 행

public function exportPartsAuthority($fileArray) 
{  
    // Do whatever - sample code for a webservice request below. 
    foreach ($fileArray as $filename => $fileContent) { 

     // Do nothing 

    } 

    foreach(explode("\n",$fileContent) as $line){ 
     $item=explode(",",$line); 
     file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true)); 
    } 

} 

값은 아래에 볼 수 있습니다 ...

"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","", 
"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","", 
"100000003","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","2P-225","A1", 

입니다 그리고 이것은 $ 포함한 FileContent 폭발 후 내 파일에 나오는 어떻게입니다 ...

Array 
(
[0] => "100000002" 
[1] => "flatrate_flatrate" 
[2] => "1.0000" 
[3] => "Brian" 
[4] => "" 
[5] => "" 
[6] => "" 
[7] => "" 
[8] => "Sunrise" 
[9] => "33323" 
[10] => "Florida" 
[11] => "US" 
[12] => "" 
[13] => "" 
[14] => 
"100000002" 
[15] => "flatrate_flatrate" 
[16] => "1.0000" 
[17] => "Brian" 
[18] => "" 
[19] => "" 
[20] => "" 
[21] => "" 
[22] => "Sunrise" 
[23] => "33323" 
[24] => "Florida" 
[25] => "US" 
[26] => "" 
[27] => "" 
[28] => 
"100000003" 
[29] => "flatrate_flatrate" 
[30] => "1.0000" 
[31] => "Brian" 
[32] => "" 
[33] => "" 
[34] => "" 
[35] => "" 
[36] => "Sunrise" 
[37] => "33323" 
[38] => "Florida" 
[39] => "US" 
[40] => "2P-225" 
[41] => "A1" 
[42] => 
) 

해당 문자열에서 각 줄을 자체 배열로 생성하려면 어떻게해야합니까? 이미 explode()에 전화로 각 라인에 대한 새로운 배열을 만들로

+0

입력 데이터에 결함이 있으므로 먼저 수정하십시오. – Populus

+0

개인 정보이므로 많은 데이터가 숨겨져 있습니다. 그렇지 않으면 어떻게 결함이 있습니까? –

답변

0

당신은 실제로 정말 가까이있어 - 당신은 그냥 보유하는 변수를 필요로하는 모든 $item의 당신이 만드는, 그리고 추가 $item에 반복을 통해 반복 할 때마다

function exportPartsAuthority($fileArray) {  

    //this would hold your output 
    $arrayOfArrays = array(); 

    foreach ($fileArray as $filename => $fileContent) { 

     // Do nothing 
     foreach(explode("\n",$fileContent) as $line){ 
      $item=explode(",",$line); 
      file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true)); 

      //now add it to your arrayOfArrays 
      $arrayOfArrays[] = $item; 
     } 
    } 
} 
관련 문제