foreach两次使用对象的值

时间:2018-09-04 19:59:23

标签: php arrays json geojson

我通过rest-service收到如下所示的json对象

[
 {
    "pos_time": "04.09.2018 09:57:02",
    "receivetime": "04.09.2018 09:57:18",
    "latitude": 47554898,
    "longitude": 13173448,
    "speed": 8,
    "course": 359,
    "country": "AT"
  },
  {
    "pos_time": "04.09.2018 09:58:02",
    "receivetime": "04.09.2018 09:58:31",
    "latitude": 47835502,
    "longitude": 13653503,
    "speed": 7,
    "course": 174,
    "country": "AT"
  },
]

形成这个json我想创建一个geojson“ linestring”。 “线串”不是问题。问题是,我必须使用每个对象的两个坐标来创建“线串” 而且我不知道如何遍历对象并使用第二个对象和第一个对象的坐标来创建“线串”

结果应如下所示:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "pos_time": "04.09.2018 09:56:22",
        "receivetime": "04.09.2018 09:57:18",
        "course": 177,
        "speed": 2,
        "country": "AT",
        "error": null
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            13.173448, // object 1
            47.554898  // object 1
          ],
          [
            13.653503, // object 2
            47.835502  // object 2
          ]
        ]
      }
    }
  ]
}

目前的代码如下:

        // create geojson

    $geojson = array(
        'type' => 'FeatureCollection',
        'features' => array()
    );

    ///////

    foreach ($tomtom_request_array as $key => $value) {

        if (empty($value['longitude']) || empty($value['latitude']))
        {
            $longitude = "13.07202";
            $latitude = "47.889486";
            $error = "Missing or incorrect GPS data";
        }
        else
        {
            $latitude = $value['longitude']; // object 1
            $longitude = $value['latitude']; // object 1

            $latitude_previous = $value['longitude']; // object 2
            $longitude_previous = $value['latitude']; // object 2

            $error = NULL;
        }

        if (empty($value['speed']))
        {
            $speed = "0";
        }
        else
        {
            $speed = $value['speed'];
        }

        if (empty($value['course']))
        {
            $course = "0";
        }
        else
        {
            $course = $value['course'];
        }

        $feature = array(
                'type' => 'Feature',
                'properties' => array(
                'pos_time' => $value['pos_time'],
                'receivetime' => $value['receivetime'],
                'course' => $course,
                'speed' => $speed,
                'country' => $value['country'],
                'error' => $error
            ),
                'geometry' => array(
                    'type' => 'LineString',

                    'coordinates' => array(
                        array(($latitude * 0.000001), ($longitude* 0.000001)),
                        array(($latitude_previous * 0.000001), ($longitude_previous* 0.000001))
                        ),            
                    ),
        );
        array_push($geojson['features'], $feature);
    }
    $this->response($geojson, $response_status);
}

提前谢谢!

1 个答案:

答案 0 :(得分:1)

如果您的Json响应确实如您所说,那么这是我的基本要求。您只需要使用相应的索引来检索所需的数据: 例如:

鉴于您的JSON响应,您可以像这样进行操作:

 input signed [INPUT_WIDTH-1 : 0 ]  x_in, // implicit wire logic
 input logic signed [INPUT_WIDTH-1 : 0 ]  x_in, // implicit wire
 input wire signed [INPUT_WIDTH-1 : 0 ]  x_in, // implicit logic
 input wire logic signed [INPUT_WIDTH-1 : 0 ]  x_in, // explicit

在此步骤中,$tomtom_request_array=json_decode('[ { "pos_time": "04.09.2018 09:57:02", "receivetime": "04.09.2018 09:57:18", "latitude": 47554898, "longitude": 13173448, "speed": 8, "course": 359, "country": "AT" }, { "pos_time": "04.09.2018 09:58:02", "receivetime": "04.09.2018 09:58:31", "latitude": 47835502, "longitude": 13653503, "speed": 7, "course": 174, "country": "AT" } ]',true) ; $geojson = array( 'type' => 'FeatureCollection', 'features' => array() ); /////// foreach ($tomtom_request_array as $key => $value) { if (empty($value['longitude']) || empty($value['latitude'])) { $longitude = "13.07202"; $latitude = "47.889486"; $error = "Missing or incorrect GPS data"; } else { $latitude = $tomtom_request_array[0]['longitude']; // object 1 $longitude = $tomtom_request_array[0]['latitude']; // object 1 $latitude_previous =$tomtom_request_array[1]['longitude']; // object 2 $longitude_previous =$tomtom_request_array[1]['latitude']; // object 2 $error = NULL; } if (empty($value['speed'])) { $speed = "0"; } else { $speed = $value['speed']; } if (empty($value['course'])) { $course = "0"; } else { $course = $value['course']; } $feature = array( 'type' => 'Feature', 'properties' => array( 'pos_time' => $value['pos_time'], 'receivetime' => $value['receivetime'], 'course' => $course, 'speed' => $speed, 'country' => $value['country'], 'error' => $error ), 'geometry' => array( 'type' => 'LineString', 'coordinates' => array( array(($latitude * 0.000001), ($longitude* 0.000001)), array(($latitude_previous * 0.000001), ($longitude_previous* 0.000001)) ), ), ); array_push($geojson['features'], $feature); } 包含:

$geojson