【问题标题】:foreach use value from object twiceforeach 两次使用来自对象的值
【发布时间】:2018-09-04 19:59:23
【问题描述】:

我通过休息服务收到如下 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);
}

提前致谢!

【问题讨论】:

    标签: php arrays json geojson


    【解决方案1】:

    如果您的 Json 响应真的如您所说,那么它是基本的,亲爱的。您只需要使用相应的索引来检索所需的数据: 示例:

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

    $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 包含:

    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
                                            )
    
                                    )
    
                            )
    
                    )
    
            )
    
    ) 
    

    【讨论】:

    • 我尝试过这样的事情,但我做错了。非常感谢!
    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 2015-04-05
    • 2016-04-16
    • 2012-10-19
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多