php json

Trying to get the json converted to csv and into a file.
keep getting errors, help would be greatly appreciated…
Warning: join(): Invalid arguments passed in /root/foo.php on line 7
PHP Warning: join(): Invalid arguments passed in /root/foo.php on line 7
[php]

<?php $file = file_get_contents('http://tvnweather.com/chasers.json'); $json = json_decode($file); $csvfile = fopen('file.csv', 'w+'); foreach ($json as $row) { $line = "'" . join("\",\"", $row) . "\"\n"; fputs($csvfile, $line); } fclose($csvfile); ? [/php]

Have you seen the fputcsv() function?

To answer your question, $row is an associative array so you can’t just join the values that way. You would have to loop through the key => value pairs. For example:

[php]
foreach ($json as $row) {
$values = array();
foreach($row as $key => $value) {
$values[] = $value;
}
$line = “”" . join("","", $values) . “”" . PHP_EOL;
echo $line;
}
[/php]

You should also note that since you have an additional array as $value it will cause an array to string conversion warning and input “Array” to your CSV.

Sponsor our Newsletter | Privacy Policy | Terms of Service