Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

driveToJson.php 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. header('Content-type: application/json');
  3. // Function to convert CSV into associative array
  4. function csvToArray($file, $delimiter) {
  5. if (($handle = fopen($file, 'r')) !== FALSE) {
  6. $i = 0;
  7. while (($lineArray = fgetcsv($handle, 4000, $delimiter, '"')) !== FALSE) {
  8. for ($j = 0; $j < count($lineArray); $j++) {
  9. $arr[$i][$j] = $lineArray[$j];
  10. }
  11. $i++;
  12. }
  13. fclose($handle);
  14. }
  15. return $arr;
  16. }
  17. function formatArray ($feed) {
  18. $keys = array();
  19. $newArray = array();
  20. $data = csvToArray($feed, ',');
  21. $count = count($data) - 1;
  22. $labels = array_shift($data);
  23. foreach ($labels as $label) {
  24. $keys[] = $label;
  25. }
  26. $keys[] = 'id';
  27. for ($i = 0; $i < $count; $i++) {
  28. $data[$i][] = $i;
  29. }
  30. for ($j = 0; $j < $count; $j++) {
  31. $d = array_combine($keys, $data[$j]);
  32. $newArray[$j] = $d;
  33. }
  34. return $newArray;
  35. }
  36. $feed = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vQUspPunAmkZrtfhMgMvGYGTK_4ONsCnSoe6OcTFc-4ILRHTatdPIwgRx1eRM1qFTkYFMrvcx8OI4cv/pub?output=csv';
  37. $data = formatArray($feed);
  38. $info = json_encode($data);
  39. $infoAsJson = json_decode($info);
  40. $handle = fopen("renglones.json", 'w');
  41. fwrite($handle, $info);
  42. for ($i = 0; $i < count($infoAsJson); $i++) {
  43. $renglon = formatArray($infoAsJson[$i]->Link);
  44. $renglonInfo = json_encode($renglon);
  45. $renglonAsJson = json_decode($renglonInfo);
  46. $my_file = $infoAsJson[$i]->Renglones . ".json";
  47. $handle = fopen($my_file, 'w') or die ('Cannot open file');
  48. fwrite($handle, $renglonInfo);
  49. }
  50. ?>