2013-10-07 144 views
0

我正在使用下面的代碼來更改我的數據數組(逗號分隔,但不是從文件)到可以使用的數組。我的代碼如下...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)); 
    } 

} 

$ fileContent的值下面看到...

"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] => 
) 

我該如何去處理這個字符串的每一行作爲它自己的數組?

+0

您的輸入數據已存在缺陷,請先修復此問題 – Populus

+0

我隱藏了大量數據,因爲它是個人信息。它有什麼缺陷? –

回答

0

你實際上非常接近,因爲你已經創建與您的通話每行一個新的陣列explode() - 你只需要一個變量來保存所有$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; 
     } 
    } 
}