drjltlm156790 2018-09-04 19:59
浏览 80
已采纳

foreach使用对象的值两次

I recieve json objects like the following over an rest-service

[
 {
    "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"
  },
]

form this json I want to create a geojson "linestring". The "linestring" is not the problem. the problem is, that I have to use both coordinates of each object to create the "linestring" and I have no idea how to loop through the objects and use the coordinates from the second object and first object to create the "linestring"

the result should look like this:

{
  "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
          ]
        ]
      }
    }
  ]
}

and the code for the moment looks like this:

        // 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);
}

thanks in advance!

  • 写回答

1条回答 默认 最新

  • drnycqxwz63508434 2018-09-04 22:48
    关注

    if your Json response is really as you said so it is elementary my dear.You just need to use respective index to retrieve the desired data: example:

    given your JSON response you can proceed like this:

    $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);
        }
    

    At this step $geojson contains :

    print_r($geojson);
    Array
    (
        [type] => FeatureCollection
        [features] => Array
            (
                [0] => Array
                    (
                        [type] => Feature
                        [properties] => Array
                            (
                                [pos_time] => 04.09.2018 09:57:02
                                [receivetime] => 04.09.2018 09:57:18
                                [course] => 359
                                [speed] => 8
                                [country] => AT
                                [error] => 
                            )
    
                        [geometry] => Array
                            (
                                [type] => LineString
                                [coordinates] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => 13.173448
                                                [1] => 47.554898
                                            )
    
                                        [1] => Array
                                            (
                                                [0] => 13.653503
                                                [1] => 47.835502
                                            )
    
                                    )
    
                            )
    
                    )
    
                [1] => Array
                    (
                        [type] => Feature
                        [properties] => Array
                            (
                                [pos_time] => 04.09.2018 09:58:02
                                [receivetime] => 04.09.2018 09:58:31
                                [course] => 174
                                [speed] => 7
                                [country] => AT
                                [error] => 
                            )
    
                        [geometry] => Array
                            (
                                [type] => LineString
                                [coordinates] => Array
                                    (
                                        [0] => Array
                                            (
                                                [0] => 13.173448
                                                [1] => 47.554898
                                            )
    
                                        [1] => Array
                                            (
                                                [0] => 13.653503
                                                [1] => 47.835502
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    ) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)