こんにちは私はcssファイルに通常のものよりdifferrent配列構造を持つJSONファイルを変換しようとしています。私はそれをcsvファイルに変換する解決策を見つけようとしていましたが、解決策を見つけることはできません。どのようにJsonをCSVにPHPで変換する
<?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を使用している場合、私は文字列の変換にエラー配列を取得していますがとアレイは、csvファイルに代わり、実際のデータの値として印刷されていませんでした。
私は2つのforeachを使用している場合は、私が(エラーfputcsvを取得していますが)、パラメータ2が
指定された配列の文字列であることを期待PHPExcelライブラリーに使用
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
)
)
(文字列にauthor_ *を変換する)最初の段階で配列を取り除くと、それが文字列に配列をスローしません。エラー – moped
@ mopedコメントありがとうございます、少し詳細を与えることができますか?私は手動でキー値なしですべての配列を変換するか、それを行う方法があります。私と一緒に私はちょうどPHPを学び始めた。 – miststudent2011
私の答えをチェックし、何かが不明な場合はコメントに尋ねてください。 – moped