Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中json到csv的转换_Php_Json_Csv - Fatal编程技术网

php中json到csv的转换

php中json到csv的转换,php,json,csv,Php,Json,Csv,我从一个网站得到一个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

我从一个网站得到一个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”的响应;打印($josn\u 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
            )

    )
<?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);

?>
数组
(
[resultCount]=>5
[结果]=>数组
(
[0]=>阵列
(
[artistId]=>673560392
[艺人姓名]=>汉娜·拉斯金
[种类]=>电子书
[价格]=>2.99
[说明]=>Yelp Help,由专业美食评论家汉娜·拉斯金(西雅图周刊,达拉斯观察家)撰写。
[货币]=>美元
[类型]=>数组
(
[0]=>食谱、食物和葡萄酒
[1] =>书籍
[2] =>计算机与互联网
[3] =>互联网
)
[genreIds]=>阵列
(
[0] => 9028
[1] => 38
[2] => 9027
[3] => 10020
)
[发布日期]=>2013-07-06T07:00:00Z
[trackId]=>673560390
[trackName]=>Yelp帮助:如何撰写优秀的在线餐厅评论
[formattedPrice]=>2.99美元
[artistIds]=>数组
(
[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帮助:如何撰写优秀的在线餐厅评论
[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] =>阵列
(
[artistId]=>413390948
[artistName]=>Gradiva Couzin和Jennifer Grappone
[种类]=>电子书
[价格]=>1.99
[说明]=>虽然大多数企业都知道Yelp.com等网站在线评论的重要性,但他们不知道如何掌控并帮助围绕其服务或产品展开对话。
[货币]=>美元
[类型]=>数组
(
[0]=>行业和专业
[1] =>书籍
[2] =>商业和个人金融
)
[genreIds]=>阵列
(
[0] => 10005
[1] => 38
[2] => 9009
)
[发布日期]=>2013-10-03T07:00:00Z
[trackId]=>737317739
[trackName]=>Yelp for Business
[formattedPrice]=>1.99美元
[artistIds]=>数组
(
[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文件时,我得到以下错误

第29行D:\wamp\www\search\u book\search\u code.php中的数组到字符串转换

请引导我。。我不熟悉这个

试试这个:)


fputcsv只接受一维数组,不接受多维数组。您必须将
流派
流派
艺术类
等转换为字符串。也许您需要类似于
内爆()的方法

例如:


因此,在csv中,您的
genres
列类似于
Cookbooks,Food&Wine | Books | Business&Personal Finance

,这不起作用,因为我的回答是复杂的,而且只是简单的json数组示例。
foreach($josn_decoded['results'] as $search_result) {
    if (is_array($search_result)) {
        $search_result = implode('|', $search_result);
    }

    fputcsv($fp, $search_result);
}