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