简体   繁体   中英

Parse JSON with PHP and output to CSV

I'm wondering how I can use PHP to parse the JSON below and output the parsed values into CSV format.

{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"98765","88888":"98765"}
}

I would like to extract just the keys from the test array (ie 12345 , 56789 ) and have them returned in CSV format. Is that possible to do using json_decode?

Please feel free to ask any questions if you need more information

$json = json_decode($json, true);
fputcsv(fopen('file', 'w'), array_keys($json['test']));
$s = '{
 "test":{"12345":"98765","56789":"54321"},
 "control":{"99999":"98765","88888":"98765"}
}';


$json = json_decode($s, true);
echo implode("\n", array_keys($json['test']));

Would that do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM