gpt4 book ai didi

php - 如何在 php 中将 Json 转换为 CSV

转载 作者:可可西里 更新时间:2023-10-31 23:27:27 25 4
gpt4 key购买 nike

您好,我正在尝试将具有不同于普通数组结构的 json 文件转换为 csv 文件。我一直在尝试找到将其转换为 csv 文件的解决方案,但我找不到解决方案。

  <?php 
$jsondata = '[{
"accession_number_original": "2012.11.45",
"author_birth_date": [
"1932"
],
"author_date": [
"1932"
],
"author_death_date": [
""
],
"author_description": [
"American"
],
"author_display": [
"Day yon"
],
"author_names_first": [
"Day"
],
"author_names_last": [
"yon"
],
"author_names_middle": [
""
],
"image_height": "12 1/2",
"image_width": "18 1/4",
"jp2_image_url": "",
"location_physical_location": "Art Gallery",
"location_shelf_locator": "Unknown",
"master_image_url": "",
"note_provenance": "Gift of Gary Ginsberg and Susanna Aaron",
"object_date": "1963/2010",
"object_depth": "",
"object_height": "",
"object_width": "",
"origin_datecreated_end": "1963",
"origin_datecreated_start": "1963",
"physical_description_extent": [
"12 1/2 x 18 1/4"
],
"physical_description_material": [
"Gelatin silver print"
],
"physical_description_technique": [
"Gelatin silver print"
],
"pid": "bdr:123456",
"title": "As Demonstrators"
}]';

$jsonDecoded = json_decode($jsondata);
print_r('<pre>');
print_r($jsonDecoded);
print_r('</pre>');
$fh = fopen('fileout.csv', 'w');
if (is_array($jsonDecoded)){
print_r('<-------- line variable output-------->');
foreach($jsonDecoded as $line){
print_r('<pre>'); print_r($line); print_r('</pre>');
print_r('<-------- data variable output-------->');
if (is_array($line)||is_object($line)){
foreach($line as $data){
fputcsv($fp,$data);
}
}
}
}
}

fclose($fh);
print_r('Converted Successfully');
?>

我试着研究了 stackoverflow 中的大多数类似问题,但没有一个有我这样的数组,所以它们对我没有多大用处。

如果我使用单个 foreach,我会收到错误消息Array to String Conversion failed 并且 Array 正在作为值而不是实际数据打印到 csv 文件中。

如果我使用两个 foreach 我会得到错误 fputcsv() expects parameter 2 to be array string given

json解码后的var_dump或print_r结果如下

Array
(
[0] => stdClass Object
(
[accession_number_original] => 2012.11.45
[author_birth_date] => Array
(
[0] => 1932
)

[author_date] => Array
(
[0] => 1932
)

[author_death_date] => Array
(
[0] =>
)

[author_description] => Array
(
[0] => American
)

[author_display] => Array
(
[0] => Day yon
)

[author_names_first] => Array
(
[0] => Day
)

[author_names_last] => Array
(
[0] => yon
)

[author_names_middle] => Array
(
[0] =>
)

[image_height] => 12 1/2
[image_width] => 18 1/4
[jp2_image_url] =>
[location_physical_location] => Art Gallery
[location_shelf_locator] => Unknown
[master_image_url] =>
[note_provenance] => Gift of Gary Ginsberg and Susanna Aaron
[object_date] => 1963/2010
[object_depth] =>
[object_height] =>
[object_width] =>
[origin_datecreated_end] => 1963
[origin_datecreated_start] => 1963
[physical_description_extent] => Array
(
[0] => 12 1/2 x 18 1/4
)

[physical_description_material] => Array
(
[0] => Gelatin silver print
)

[physical_description_technique] => Array
(
[0] => Gelatin silver print
)

[pid] => bdr:123456
[title] => As Demonstrators
)
)

最佳答案

正如我在评论中提到的,第一步是处理数组值,因此每一行都需要转换值(它只计算你提供的格式,如果有一个包含 2 个值的数组,只有第一个将被传递到 csv)。

您修改后的源代码:

$jsonDecoded = json_decode($jsondata, true); // add true, will handle as associative array    
print_r('<pre>');
print_r($jsonDecoded);
print_r('</pre>');
$fh = fopen('fileout.csv', 'w');
if (is_array($jsonDecoded)) {
print_r('<-------- line variable output-------->');
foreach ($jsonDecoded as $line) {
// with this foreach, if value is array, replace it with first array value
foreach ($line as $key => $value) {
if (is_array($value)) {
$line[$key] = $value[0];
}
}
print_r('<pre>'); print_r($line); print_r('</pre>');
// no need for foreach, as fputcsv expects array, which we already have
if (is_array($line)) {
fputcsv($fh,$line);
}
}
}
fclose($fh);
print_r('Converted Successfully');

执行后的脚本输出:

[output of your print_r($jsonDecoded);]

<-------- line variable output-------->

Array
(
[accession_number_original] => 2012.11.45
[author_birth_date] => 1932
[author_date] => 1932
[author_death_date] =>
[author_description] => American
[author_display] => Day yon
[author_names_first] => Day
[author_names_last] => yon
[author_names_middle] =>
[image_height] => 12 1/2
[image_width] => 18 1/4
[jp2_image_url] =>
[location_physical_location] => Art Gallery
[location_shelf_locator] => Unknown
[master_image_url] =>
[note_provenance] => Gift of Gary Ginsberg and Susanna Aaron
[object_date] => 1963/2010
[object_depth] =>
[object_height] =>
[object_width] =>
[origin_datecreated_end] => 1963
[origin_datecreated_start] => 1963
[physical_description_extent] => 12 1/2 x 18 1/4
[physical_description_material] => Gelatin silver print
[physical_description_technique] => Gelatin silver print
[pid] => bdr:123456
[title] => As Demonstrators
)

Converted Successfully

关于php - 如何在 php 中将 Json 转换为 CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47262109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com