2015-10-19 57 views
1

更新:我更改了代碼:如何將json的輸出寫入CSV?

第一個關鍵字正在工作,第二個不寫入csv。多維數組被跳過並寫爲「Array」。沒關係。更新的代碼現在看起來像這樣:

$josn_decoded = json_decode($output, true); 
$file_name = "searched_book.csv"; 
$fp = fopen($file_name, 'w'); 


foreach($josn_decoded['results'] as $search_result){ 





    fputcsv($fp, $search_result); 
    } 

fclose($fp); 

如何在CSV中以行記錄更多關鍵字?

更新2:

這裏是輸出(抱歉,我不能讓它顯示在另一種方式):

 Notice: Array to string conversion in /volume2/web/static/buzzsumo/index.php on line 78 
     array(25) { 
      ["published_date"]=> int(1424081166) 
      ["linkedin_shares"]=> int(43) 
      ["google_plus_shares"]=> int(0) 
      ["meta_keywords"]=> string(27) "payments technology company" 
      ["total_shares_with_pinterest"]=> int(48) 
      ["article_types"]=> array(1) { 
        [0]=> string(15) "general_article" } 
      ["twitter_shares"]=> int(5) 
      ["og_url"]=> string(131) "http://www.thepaypers.com//online-payments/pay-on-launches-open-payment-platform-for-white-label-payment-gateway-solutions/758641-3" 
      ["url"]=> string(131) "http://www.thepaypers.com//online-payments/pay-on-launches-open-payment-platform-for-white-label-payment-gateway-solutions/758641-3" 
      ["pinterest_shares"]=> int(0) 
      ["id"]=> string(9) "405519905" 
      ["total_shares"]=> int(48) 
      ["title"]=> string(79) "PAY.ON launches Open Payment Platform for white label payment gateway solutions" 
      ["thumbnail"]=> string(51) "http://www.thepaypers.com/images/linkedin-share.png" 
      ["subdomain"]=> string(18) "www.thepaypers.com" 
      ["num_words"]=> int(206) 
      ["domain_name"]=> string(14) "thepaypers.com" 
      ["total_facebook_shares"]=> int(0) 
      ["giveaway"]=> int(0) 
      ["infographic"]=> int(0) 
      ["general_article"]=> int(1) 
      ["guest_post"]=> int(0) 
      ["interview"]=> int(0) 
      ["video"]=> int(0) 
      ["display_title"]=> string(254) "PAY.ON launches Open Payment Platform for white label payment gateway solutions" 
      } 

的article_types實際上可以刪除,因爲在底部有相同再次。此外,我需要第一列中的關鍵字($ search_result)。

謝謝。

+0

[轉換JSON爲CSV格式使用PHP]的可能的複製(http://stackoverflow.com/questions/20667418/converting-json-to-csv- format-using-php) – Wobbles

+0

@Wobbles - 我試過,但沒有奏效。 –

+1

請記住,如果我問CSV的原因? JSON通常更容易與跨平臺和語言一起工作。 – Wobbles

回答

0
$json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}"; 

$out = fopen('file.csv', 'w'); 
foreach(json_decode($json, true)['data'] as $key => $value) { 
    fputcsv($out, $value); 
} 
fclose($out); 

    fclose($fp); 
+2

歡迎來到SO!爲了幫助OP,請包括解釋爲什麼您的答案可行,以便未來的觀衆能夠理解您的答案。 –

+0

感謝Rohan。不幸的是我在這裏得到一個錯誤: 未定義的變量:第65行的json 警告:提供給第65行的foreach()的無效參數。 我將發佈數組,以便您看到輸出。 –

+0

@Rohan:第一個錯誤消失了,第二個是foreach()錯誤,我想我需要找出它是一個字符串還是一個int,然後正確寫入它。我嘗試了一切它不會工作。 –

0

正如你可以看到嵌套在每個結果中的一個元素是一個數組。如果您嘗試將數組寫入CSV,它將被轉換爲文本「數組」,因爲CSV不支持這種情況。你必須寫CSV之前對待這種情況:

foreach ($josn_decoded['results'] as $search_result) 
{ 
    $search_result['article_types'] = implode(', ', $search_result['article_types']); 
    fputcsv($fp, $rearch_result); 
}