如何使用 fgetcsv 检测最后一行?

How do I detect the last line using fgetcsv?

我正在尝试将 csv 文件转换为 GeoJSON,然后在 Google 地图上显示结果。

特征集合中的最后一个特征后面不应该有逗号,所以我需要检测哪一行是 fgetcsv 中的最后一行并省略了逗号,我正在努力解决这个问题。

这是我目前的代码:

<?php   
     $file = fopen("Incident_List.csv","r");

     echo "{<br>";
     echo "\"type\": \"FeatureCollection\",<br>";
     echo "\"features\": [<br>";

    $csvflag = true;
    while(! feof($file))
      {
      while (($data = fgetcsv($file)) !== FALSE) {
        if($csvflag) { $csvflag = false; continue; }
        echo "{<br>";
            echo "\"geometry\": {<br>";
            echo "\"type\": \"Point\",<br>";
            echo "\"coordinates\": [<br>";
        echo $data[2] . "," . $data[1] . "<br>";
        echo "]<br>";
        echo "},<br>";
        echo "\"type\": \"Feature\",<br>";
            echo "\"properties\": {<br>";
            echo "\"Category\":\"" . $data[4] . "\",<br>";
            echo "\"Grid Reference\":\"" . $data[0] . "\",<br>";
            echo "\"Day\":\"" . $data[3] . "\",<br>";
            echo "\"Date\":\"" . $data[4] . "\",<br>";
            echo "\"Type Of Incident\":\"" . $data[5] . "\",<br>";
            echo "\"Area\":\"" . $data[7] . "\",<br>";
            echo "\"Sex\":\"" . $data[8] . "\",<br>";
            echo "\"Age\":\"" . $data[9] . "\",<br>";
            echo "\"Members\":\"" . $data[10] . "\",<br>";
            echo "\"Hours\":\"" . $data[11] . "\",<br>";
            echo "\"Notes\":\"" . $data[12] . "\",<br>";
            echo "}<br>";
            echo "},<br>";

    }       

      }
    fclose($file);
      ?>

这是我试图实现的输出示例:

{
  "type": "FeatureCollection",
  "features": [
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          -3.224752,
          55.891831
        ]
      },
      "type": "Feature",
      "properties": {
        "category": "misper",
        "hours": "10am - 6pm",
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquam erat non tellus pellentesque, non euismod quam lacinia. Sed ac ex non eros aliquam sagittis. Etiam iaculis turpis sit amet arcu viverra, ac cursus sem feugiat. Sed fermentum dolor non mauris semper, eu pulvinar dolor feugiat.",
        "name": "Missing Person",
        "phone": "+44 20 1234 5678"
      }
    },
    {
      "geometry": {
        "type": "Point",
        "coordinates": [
          -2.631056,
          55.243980
        ]
      },
      "type": "Feature",
      "properties": {
        "category": "walker",
        "hours": "10am - 6pm",
        "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam aliquam erat non tellus pellentesque, non euismod quam lacinia. Sed ac ex non eros aliquam sagittis. Etiam iaculis turpis sit amet arcu viverra, ac cursus sem feugiat. Sed fermentum dolor non mauris semper, eu pulvinar dolor feugiat.",
        "name": "Injured Walker",
        "phone": "+44 20 1234 5678"
      }
    }
  ]
}

感谢您的帮助。

我会建议更像这样的东西,而不是手动写出你自己的 JSON。

将数据附加到 PHP 数组,并使用内置 json_encode 函数创建 JSON.

$file = fopen("Incident_List.csv","r");

$features = [];

while (false !== $data = fgetcsv($file)) {
    $geometry = [
        'type' => 'Point',
        'coordinates' => [$data[2], $data[1]]
    ];
    $properties = [
        'Category' => $data[4],
        'Grid Reference' => $data[0],
        'Day' => $data[3],
        'Date' => $data[4],
        'Type of Incident' => $data[5],
        'Area' => $data[7],
        'Sex' => $data[8],
        'Age' => $data[9],
        'Members' => $data[10],
        'Hours' => $data[11],
        'Notes' => $data[12]
    ];

    $features[] = [
        'geometry' => $geometry, 
        'type' => 'Feature',
        'properties' => $properties
    ];
}

echo json_encode(['type' => 'FeatureCollection','features' => $features]);