2013-05-08 52 views
1

我想從JSON(下面)中取值並將它們插入到現有CSV的新列中。用我現在的代碼(也在下面),添加到新行的所有內容都是每行上的「數組」一詞。我如何調整我的代碼以使其正常工作?另外,是否有一種方法可以爲我創建的新列設置標題名稱?拉JSON值並添加到現有CSV中的新列

謝謝你,請看下面的代碼,並讓我知道如果你有任何問題。

JSON斑點:

{ 
"test":{"12345":"98765","56789":"54321"}, 
"control":{"99999":"987651","88888":"987652","22222":"987653","27644":"987655"} 
} 

電流CSV:

userid,foo_date,bar,baz,qux,country 
"12345","2013-03-14","1.72500","1","1055.74","UK" 
"38726","2013-03-14",\N,"1","3430.07","UK" 
"85127","2013-03-14",\N,"0","635.25","US" 
"16984","2013-03-14",\N,"1","5233.09","US" 

電流PHP(按照How to add columns to CSV using PHP):

$json = json_decode($s, true); 
$json_values = array_values($json['control']); 

$newCsvData = array(); 

if (($handle = fopen("testgroup.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     $data[] = $json_values; 
     $newCsvData[] = $data; 
    } 
    fclose($handle); 
} 

$handle = fopen('testgroup.csv', 'w'); 

foreach ($newCsvData as $line) { 
    fputcsv($handle, $line); 
} 

fclose($handle); 

$s等於JSON團塊。

得到的CSV應該是這樣的:

userid,foo_date,bar,baz,qux,country,extra 
"12345","2013-03-14","1.72500","1","1055.74","UK","987651" 
"38726","2013-03-14",\N,"1","3430.07","UK","987652" 
"85127","2013-03-14",\N,"0","635.25","US","987653" 
"16984","2013-03-14",\N,"1","5233.09","US","987655" 
+0

預期的結果.csv文件應該如何看起來像?順便說一句,你已經發布了這個問題兩次。爲什麼不只是加強舊的職位? – hek2mgl 2013-05-08 01:12:14

+0

我剛剛添加了如何生成的CSV文件應該看看我的問題。我決定不提高最後的帖子,因爲它被標記爲重複(我認爲不公平)。這篇文章有更多的信息。 – 585connor 2013-05-08 01:20:06

+0

然後你應該刪除舊帖子 – hek2mgl 2013-05-08 01:20:31

回答

2

試一下這個(測試):

$data = <<<EOF 
{ 
"test":{"12345":"98765","56789":"54321"}, 
"control":{"99999":"987651","88888":"987652","22222":"987653","27644":"987655"} 
} 
EOF; 

$json = json_decode($data, true); 
$json_values = array_values($json['control']); 
// add column header to the start of the array 
array_unshift($json_values, 'extra'); 

// open the file in read write mode. 
$fd = fopen('testgroup.csv', 'r+'); 
// add the last field for each record 
$records = array(); 
while($record = fgetcsv($fd)) { 
    $record []= array_shift($json_values); 
    $records []= $record; 
} 

// clear file and set seek to start 
ftruncate($fd, 0); 
fseek($fd, 0); 

// rewrite file 
foreach($records as $record) { 
    fputcsv($fd, $record); 
} 

fclose($fd); 
+0

我在這一行上得到了一個解析錯誤:$ json_values = array_values($ json ['control']);'。錯誤是第8行的「解析錯誤:解析錯誤,期望'T_STRING'或'T_VARIABLE'或'T_NUM_STRING'foo.php」 – 585connor 2013-05-08 01:53:00

+0

也許您無法正確複製/粘貼它。代碼工作(現在測試兩次) – hek2mgl 2013-05-08 01:58:54

+0

這很奇怪。我想我沒有複製/粘貼不正確。感謝您的幫助! – 585connor 2013-05-08 02:19:38

0

另一個快速和簡單的方式來做到這一點是:

$json = json_decode($s, true); 
$json_values = array_values($json['control']); 

$i = 0; 
if (($handle = fopen("csvFile.csv", "r")) !== FALSE) { 
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
     //seperate your header data 
     $i++; 
     if($i==1) 
      $header = $data; 
     else 
      $datas[] = $data; //all the data 
     $i++; 
    } 
    fclose($handle); 
} 

//adding the json now to the data based on the test value 
// assuming that your json data length will be same as number of rows in csv 
//you can modify the code easily to make it dynamic 
$j=0; 
foreach($datas as &$d){ 
    array_push($d, $json_values[$j]); 
    $j++;  
} 

//than append data in the file 
$handle = fopen('testgroup.csv', 'r+'); 
//put the header inside the csv 
fputcsv($handle, $header); 

foreach ($datas as $line) { 
    fputcsv($handle, $line); 
} 

fclose($handle); 

這是第一次一旦你能夠找到一個更好的方法來做到這一點,就像@ hek2mgl建議的那樣, t想把它扔出去......

Dins