foreach 使用对象中的值两次
foreach use value from object twice
我通过休息服务收到 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"。
"linestring" 不是问题。问题是,我必须使用每个对象的两个坐标来创建 "linestring"
而且我不知道如何遍历对象并使用第二个对象和第一个对象的坐标来创建 "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);
}
提前致谢!
如果您的 Json 响应确实如您所说,那么它是基本的,我 dear.You 只需要使用各自的索引来检索所需的数据:
示例:
根据您的 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
)
)
)
)
)
)
我通过休息服务收到 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"。 "linestring" 不是问题。问题是,我必须使用每个对象的两个坐标来创建 "linestring" 而且我不知道如何遍历对象并使用第二个对象和第一个对象的坐标来创建 "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);
}
提前致谢!
如果您的 Json 响应确实如您所说,那么它是基本的,我 dear.You 只需要使用各自的索引来检索所需的数据: 示例:
根据您的 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
)
)
)
)
)
)