Reputation: 1283
i am getting a json response from a site,
then convert it into json decoded file, my code is
$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);
the response of echo "<pre>"; print_r($josn_decoded)
is like
<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
)
)
)
when i write it into csv file i get following error
Array to string conversion in D:\wamp\www\search_book\search_code.php on line 29
please guide me.. i am new to this
Upvotes: 0
Views: 95
Reputation: 6438
fputcsv only accept one dimension array, not multidimensional array. You have to convert the genres
, genreIds
, artistIds
, etc into string. Maybe you need something like implode()
method.
For example:
foreach($josn_decoded['results'] as $search_result) {
if (is_array($search_result)) {
$search_result = implode('|', $search_result);
}
fputcsv($fp, $search_result);
}
So in csv, your genres
column be like Cookbooks, Food & Wine|Books|Business & Personal Finance
.
Upvotes: 1
Reputation: 19
Try this one :)
<?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);
?>
Upvotes: 0