PHP 中的 JSON 到 CSV 转换


json to csv conversion in php

我从站点收到 JSON 响应,

然后将其转换为JSON解码文件,我的代码是

$author = $_POST['author_name'];
$author_name = str_replace(' ', '+', $author);
$term = $_POST['term'];
$term_search = str_replace(' ', '+', $term);
$country = $_POST['country'];
$entity = 'ebook';
$search_url = "https://itunes.apple.com/searchterm=".$term_search."&entity=".$entity."&country=".$country;
$data = file_get_contents($search_url);
$josn_decoded = json_decode($data, true);
$file_name = "searched_book.csv";
$fp = fopen($file_name, 'w');
foreach($josn_decoded['results'] as $search_result){
    fputcsv($fp, $search_result);
}
fclose($fp);

echo "<pre>"; print_r($josn_decoded)的反应就像

 <pre>Array
(
    [resultCount] => 5
    [results] => Array
        (
            [0] => Array
                (
                    [artistId] => 673560392
                    [artistName] => Hanna Raskin
                    [kind] => ebook
                    [price] => 2.99
                    [description] => Yelp Help, written by professional food critic Hanna Raskin (Seattle Weekly, Dallas Observer).
                    [currency] => USD
                    [genres] => Array
                        (
                            [0] => Cookbooks, Food & Wine
                            [1] => Books
                            [2] => Computers & Internet
                            [3] => Internet
                        )
                [genreIds] => Array
                    (
                        [0] => 9028
                        [1] => 38
                        [2] => 9027
                        [3] => 10020
                    )
                [releaseDate] => 2013-07-06T07:00:00Z
                [trackId] => 673560390
                [trackName] => Yelp Help: How to Write Great Online Restaurant Reviews
                [formattedPrice] => $2.99
                [artistIds] => Array
                    (
                        [0] => 673560392
                    )
                [artworkUrl60] => http://a2.mzstatic.com/us/r30/Publication/v4/30/fa/89/30fa8929-ba32-41fd-c046-bb660b2c886c/9781301327515.60x60-50.jpg
                [artistViewUrl] => https://itunes.apple.com/us/artist/hanna-raskin/id673560392?mt=11&uo=4
                [trackCensoredName] => Yelp Help: How to Write Great Online Restaurant Reviews
                [fileSizeBytes] => 219793
                [artworkUrl100] => http://a5.mzstatic.com/us/r30/Publication/v4/30/fa/89/30fa8929-ba32-41fd-c046-bb660b2c886c/9781301327515.100x100-75.jpg
                [trackViewUrl] => https://itunes.apple.com/us/book/yelp-help-how-to-write-great/id673560390?mt=11&uo=4
            )
        [1] => Array
            (
                [artistId] => 413390948
                [artistName] => Gradiva Couzin & Jennifer Grappone
                [kind] => ebook
                [price] => 1.99
                [description] => While most businesses know the importance of online reviews on sites such as Yelp.com, they have no clue how to grab the reins and help shape the conversation around their service or product. 
                [currency] => USD
                [genres] => Array
                    (
                        [0] => Industries & Professions
                        [1] => Books
                        [2] => Business & Personal Finance
                    )
                [genreIds] => Array
                    (
                        [0] => 10005
                        [1] => 38
                        [2] => 9009
                    )
                [releaseDate] => 2013-10-03T07:00:00Z
                [trackId] => 737317739
                [trackName] => Yelp for Business
                [formattedPrice] => $1.99
                [artistIds] => Array
                    (
                        [0] => 413390948
                        [1] => 413390943
                    )
                [artworkUrl60] => http://a2.mzstatic.com/us/r30/Publication6/v4/48/d4/fe/48d4fe78-2668-0c25-02f2-0e1cf2c21983/9781118857731.60x60-50.jpg
                [artistViewUrl] => https://itunes.apple.com/us/artist/gradiva-couzin-jennifer-grappone/id413390948?mt=11&uo=4
                [trackCensoredName] => Yelp for Business
                [fileSizeBytes] => 2703426
                [artworkUrl100] => http://a4.mzstatic.com/us/r30/Publication6/v4/48/d4/fe/48d4fe78-2668-0c25-02f2-0e1cf2c21983/9781118857731.100x100-75.jpg
                [trackViewUrl] => https://itunes.apple.com/us/book/yelp-for-business/id737317739?mt=11&uo=4
            )
    )

当我将其写入csv文件时,出现以下错误

数组到字符串的转换 D:''wamp''www''search_book''search_code.php 在第 29 行

请指导我..我是新手

fputcsv 只接受一维数组,不接受多维数组。你必须将genresgenreIdsartistIds等转换为字符串。也许你需要类似implode()方法。

例如:

foreach($josn_decoded['results'] as $search_result) {
    if (is_array($search_result)) {
        $search_result = implode('|', $search_result);
    }
    fputcsv($fp, $search_result);
}

所以在csv中,你的genres列就像Cookbooks, Food & Wine|Books|Business & Personal Finance

试试这个:)

<?php
    $json_str = "{'aintlist':[4,3,2,1], 'astringlist':['str1','str2']}";
    $json_obj = json_decode ($json_str);
    $fp = fopen('file.csv', 'w');
    foreach ($json_obj as $fields) 
    {
        fputcsv($fp, $fields);
    }
    fclose($fp);
?>