- <?php
- $del = "\t";
- header('Content-Type: text/csv; charset=utf-8');
- header('Content-Disposition: attachment; filename=data.txt');
- # for testing:
- #$input = '[{"BaustelleIdent":"ae08e943-2306-467d-8141-72bb93c5cb3a",
- # "NachunternehmerIdent":"3d6966da-9a34-481f-9606-55aeaa119760",
- # "BauteilIdent":null}]';
- $input = file_get_contents('php://stdin'); # used when called by command line
- if ( $input == '' ) $input = file_get_contents('json.txt'); # used when called as web application
- $arr = json_decode($input, true);
- $header = array();
- foreach ( $arr[0] as $key => $dummy ):
- $header[] = $key;
- endforeach;
- $output = fopen('php://output', 'w');
- fputcsv($output, $header, $del);
- foreach ( $arr as $col ):
- $row = array();
- foreach ( $col as $val ):
- $row[] = $val;
- endforeach;
- fputcsv($output, $row, $del);
- endforeach;
- ?>