我通过将已经存在的API转换成对自己更有用的格式来创建API输出。我要返回的数据是车辆及其估计的到达时间,这些数据将在接下来的30分钟内停止在一条路线上。但是,我只想为每个不同的车辆返回最新/最接近当前时间。最终,这将输出其位置。
当前输出将为每辆车显示多个ETA。我只想为每个独特的车辆返回最接近的预计到达时间。
数据数组:
“ when”键是我已经转换的UNIX时间戳,因此可以考虑到这一点来计算与当前时间最接近的时间。我只需要将最接近当前时间的不同车辆放在阵列中即可。
数据源:
[4,"1.0",1546392702443]
[1,"Rainham Road","73559","N365",1,"Havering Park","LX11BAO",1546394065000]
[1,"Aldingham Gardens","57812","N365",1,"Havering Park","LX11BAO",1546394337000]
[1,"Orchard Village / Lowen Road","47144","N365",2,"Orchard Village","LX11BAO",1546393347000]
[1,"Brian Close","49216","N365",2,"Orchard Village","LX11BFK",1546394428000]
[1,"Romford Bus Garage","59172","N365",2,"Orchard Village","LX11BFK",1546393384000]
[1,"Dunedin Road","51104","N365",1,"Havering Park","LX11BAO",1546393949000]
[1,"Havering Park","77538","N365",1,"Havering Park","LX11BJJ",1546394353000]
[1,"Lowshoe Lane","71530","N365",1,"Havering Park","LX11BJJ",1546394121000]
[1,"Mowbrays Road","55944","N365",2,"Orchard Village","LX11BFK",1546393223000]
[1,"Elm Park Station","52895","N365",2,"Orchard Village","LX11BAO",1546392761000]
[1,"Ambleside Avenue","53403","N365",1,"Havering Park","LX11BAO",1546394378000]
[1,"Collier Row Roundabout / Rex Close","48309","N365",2,"Orchard Village","LX11BFK",1546393135000]
[1,"Mercury Gardens","47576","N365",2,"Orchard Village","LX11BFK",1546393535000]
[1,"Collier Row / Nth Romford Community Ctr","51753","N365",2,"Orchard Village","LX11BFK",1546393114000]
[1,"Queen's Hospital","49960","N365",1,"Havering Park","LX11BJJ",1546393373000]
[1,"Western Road","75612","N365",1,"Havering Park","LX11BJJ",1546393642000]
[1,"Park Lane","56024","N365",1,"Havering Park","LX11BJJ",1546393162000]
[1,"Romford Station","73397","N365",2,"Orchard Village","LX11BFK",1546393698000]
[1,"Park Lane","52666","N365",2,"Orchard Village","LX11BFK",1546394085000]
[1,"Larchwood Avenue","77776","N365",1,"Havering Park","LX11BJJ",1546394265000]
[1,"South Street","74249","N365",2,"Orchard Village","LX11BAO",1546393396000]
[1,"Hyland Way","51331","N365",2,"Orchard Village","LX11BFK",1546394149000]
[1,"Hubert Road","75416","N365",2,"Orchard Village","LX11BAO",1546393137000]
[1,"Alexandra Road","77962","N365",2,"Orchard Village","LX11BAO",1546393063000]
[1,"Edmund Road","72436","N365",1,"Havering Park","LX11BAO",1546393964000]
[1,"Burland Road","71859","N365",2,"Orchard Village","LX11BFK",1546393070000]
[1,"Hainault Road","72714","N365",1,"Havering Park","LX11BJJ",1546394008000]
[1,"Hulse Avenue","76441","N365",2,"Orchard Village","LX11BFK",1546393180000]
[1,"Ambleside Avenue","74747","N365",2,"Orchard Village","LX11BAO",1546392814000]
[1,"Askwith Road","52966","N365",1,"Havering Park","LX11BAO",1546393850000]
[1,"Harrow Lodge Park","73439","N365",1,"Havering Park","LX11BJJ",1546393016000]
[1,"Brian Close","49216","N365",2,"Orchard Village","LX11BAO",1546392692000]
[1,"Ford Lane","52759","N365",1,"Havering Park","LX11BAO",1546394135000]
[1,"South Street","74249","N365",1,"Havering Park","LX11BAO",1546393810000]
[1,"Roneo Corner","55531","N365",1,"Havering Park","LX11BJJ",1546393272000]
[1,"Burland Road","75140","N365",1,"Havering Park","LX11BJJ",1546394230000]
[1,"Abbs Cross Academy","52797","N365",1,"Havering Park","LX11BJJ",1546392842000]
[1,"Romford Station","49501","N365",1,"Havering Park","LX11BJJ",1546393556000]
[1,"Elm Park Station","52895","N365",2,"Orchard Village","LX11BFK",1546394491000]
[1,"Albyns Close","74075","N365",2,"Orchard Village","LX11BAO",1546392990000]
[1,"Hainault Road","50589","N365",2,"Orchard Village","LX11BFK",1546393247000]
[1,"Stanley Road","56432","N365",2,"Orchard Village","LX11BFK",1546394319000]
[1,"Romford Market","50889","N365",2,"Orchard Village","LX11BFK",1546393496000]
[1,"Ford Lane","48503","N365",2,"Orchard Village","LX11BAO",1546393021000]
[1,"Stanley Road South","71094","N365",1,"Havering Park","LX11BAO",1546394007000]
[1,"Betterton Road","54000","N365",1,"Havering Park","LX11BAO",1546393891000]
[1,"Rom Valley Way","76837","N365",1,"Havering Park","LX11BJJ",1546393475000]
[1,"Elm Park Station","53957","N365",1,"Havering Park","LX11BAO",1546394428000]
[1,"Parklands School","56285","N365",2,"Orchard Village","LX11BFK",1546393319000]
[1,"Rainham Road","55548","N365",2,"Orchard Village","LX11BAO",1546393104000]
[1,"Rom Valley Way","48861","N365",2,"Orchard Village","LX11BFK",1546393775000]
[1,"Jersey Road","56854","N365",2,"Orchard Village","LX11BAO",1546392978000]
[1,"Roneo Corner / Tesco","50919","N365",2,"Orchard Village","LX11BFK",1546394051000]
[1,"Abbs Cross Lane","73487","N365",1,"Havering Park","LX11BJJ",1546392996000]
[1,"Elm Park Station","53957","N365",1,"Havering Park","LX11BJJ",1546392701000]
[1,"Spencer Road","51914","N365",1,"Havering Park","LX11BAO",1546393865000]
[1,"Alexandra Road","47688","N365",1,"Havering Park","LX11BAO",1546394086000]
[1,"Abbs Cross Academy","74552","N365",2,"Orchard Village","LX11BFK",1546394390000]
[1,"Cedar Road","47861","N365",1,"Havering Park","LX11BJJ",1546393887000]
[1,"Havering Park","77538","N365",2,"Orchard Village","LX11BFK",1546392970000]
[1,"Abbs Cross Gardens","50324","N365",2,"Orchard Village","LX11BFK",1546394272000]
[1,"Wood Lane","54950","N365",1,"Havering Park","LX11BAO",1546394299000]
[1,"Stanley Road","55569","N365",1,"Havering Park","LX11BJJ",1546392887000]
[1,"Mungo Park Road / South End Road","76639","N365",1,"Havering Park","LX11BAO",1546394210000]
[1,"The Avenue","56758","N365",2,"Orchard Village","LX11BFK",1546393401000]
[1,"Roneo Corner","50837","N365",2,"Orchard Village","LX11BFK",1546394010000]
[1,"Hyland Way","77235","N365",1,"Havering Park","LX11BJJ",1546393088000]
[1,"Karen Close","52831","N365",2,"Orchard Village","LX11BAO",1546393236000]
[1,"Elm Parade","56138","N365",2,"Orchard Village","LX11BFK",1546394442000]
[1,"Harrow Lodge Park","77899","N365",2,"Orchard Village","LX11BFK",1546394191000]
[1,"Queen's Hospital","57842","N365",2,"Orchard Village","LX11BFK",1546393871000]
[1,"The Avenue","75285","N365",1,"Havering Park","LX11BJJ",1546393769000]
[1,"Freeborne Gardens","49992","N365",1,"Havering Park","LX11BAO",1546394275000]
[1,"Kingaby Gardens","58099","N365",1,"Havering Park","LX11BAO",1546394234000]
[1,"Hunters Grove","56656","N365",2,"Orchard Village","LX11BFK",1546392975000]
[1,"Freeborne Gardens","47360","N365",2,"Orchard Village","LX11BAO",1546392928000]
[1,"Suttons Avenue","58972","N365",2,"Orchard Village","LX11BFK",1546394341000]
[1,"Philip Road","58662","N365",2,"Orchard Village","LX11BAO",1546393195000]
[1,"Parklands School","50482","N365",1,"Havering Park","LX11BJJ",1546393959000]
[1,"Suttons Avenue","71292","N365",1,"Havering Park","LX11BJJ",1546392861000]
[1,"Orchard Village","76755","N365",2,"Orchard Village","LX11BAO",1546393382000]
[1,"Collier Row / Nth Romford Community Ctr","59145","N365",1,"Havering Park","LX11BJJ",1546394187000]
[1,"Larchwood Avenue","48236","N365",2,"Orchard Village","LX11BFK",1546393010000]
[1,"Wood Lane","49222","N365",2,"Orchard Village","LX11BAO",1546392888000]
[1,"Aldingham Gardens","75138","N365",2,"Orchard Village","LX11BAO",1546392867000]
[1,"Hunters Grove","56619","N365",1,"Havering Park","LX11BJJ",1546394293000]
[1,"Elm Parade","58876","N365",1,"Havering Park","LX11BJJ",1546392736000]
[1,"Romford Bus Garage","55211","N365",1,"Havering Park","LX11BJJ",1546393789000]
[1,"Spencer Road","92097","N365",2,"Orchard Village","LX11BAO",1546393354000]
[1,"Western Road","55182","N365",2,"Orchard Village","LX11BFK",1546393574000]
[1,"Elm Parade","56138","N365",2,"Orchard Village","LX11BAO",1546392709000]
[1,"Hillfoot Road","49241","N365",1,"Havering Park","LX11BJJ",1546394080000]
[1,"Romford Market","49147","N365",1,"Havering Park","LX11BJJ",1546393714000]
[1,"Brian Close","49058","N365",1,"Havering Park","LX11BJJ",1546392796000]
当前代码:
$csvData = array();
$count = 0;
while (($row = fgetcsv($fh, 0, ",")) !== FALSE) {
if($count != 0){
$row = str_replace("[", "", $row);
$row = str_replace("]", "", $row);
$csvData[] = $row;
}
$count++;
}
$array = array();
foreach($csvData as $data){
$array[] = [
"stop" => $data[1],
"stopID" => $data[2],
"route" => $data[3],
"direction" => $data[4],
"dest" => $data[5],
"reg" => $data[6],
"fleet" => "00000",
"when" => date("H:i", $data[7] / 1000),
];
}
print_r($array);
我的阵列输出
Array
(
[0] => Array
(
[stop] => Rainham Road
[stopID] => 73559
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:54
)
[1] => Array
(
[stop] => Aldingham Gardens
[stopID] => 57812
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:58
)
[2] => Array
(
[stop] => Orchard Village / Lowen Road
[stopID] => 47144
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:42
)
[3] => Array
(
[stop] => Brian Close
[stopID] => 49216
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 02:00
)
[4] => Array
(
[stop] => Romford Bus Garage
[stopID] => 59172
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:43
)
[5] => Array
(
[stop] => Dunedin Road
[stopID] => 51104
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:52
)
[6] => Array
(
[stop] => Havering Park
[stopID] => 77538
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:59
)
[7] => Array
(
[stop] => Lowshoe Lane
[stopID] => 71530
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:55
)
[8] => Array
(
[stop] => Mowbrays Road
[stopID] => 55944
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:40
)
[9] => Array
(
[stop] => Elm Park Station
[stopID] => 52895
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:32
)
[10] => Array
(
[stop] => Ambleside Avenue
[stopID] => 53403
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BAO
[fleet] => 00000
[when] => 01:59
)
[11] => Array
(
[stop] => Collier Row Roundabout / Rex Close
[stopID] => 48309
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:38
)
[12] => Array
(
[stop] => Mercury Gardens
[stopID] => 47576
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:45
)
[13] => Array
(
[stop] => Collier Row / Nth Romford Community Ctr
[stopID] => 51753
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:38
)
[14] => Array
(
[stop] => Queen's Hospital
[stopID] => 49960
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:42
)
[15] => Array
(
[stop] => Western Road
[stopID] => 75612
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:47
)
[16] => Array
(
[stop] => Brian Close
[stopID] => 49058
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BAO
[fleet] => 00000
[when] => 02:01
)
[17] => Array
(
[stop] => Park Lane
[stopID] => 56024
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:39
)
[18] => Array
(
[stop] => Romford Station
[stopID] => 73397
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:48
)
[19] => Array
(
[stop] => Park Lane
[stopID] => 52666
[route] => N365
[direction] => 2
[dest] => Orchard Village
[reg] => LX11BFK
[fleet] => 00000
[when] => 01:54
)
[20] => Array
(
[stop] => Larchwood Avenue
[stopID] => 77776
[route] => N365
[direction] => 1
[dest] => Havering Park
[reg] => LX11BJJ
[fleet] => 00000
[when] => 01:57
)
)
注意:由于字符数限制,我不得不从上面的数组中删除一些数据
答案 0 :(得分:0)
这会将它们每辆车分类为一个子数组,并检查最低的时间戳:
foreach($csvData as $data) {
/* the number plate is the index */
$reg = $data[6];
/* checks if the array for the vehicle exists and compares the timestamps */
if (
is_array($array[$reg]) &&
$array[$reg]['time'] > $data[7] &&
$data[7] > (time()*1000)
) {
unset($array[$reg]);
}
/* checks if not yet is an array for the vehicle exists */
if (
!is_array($array[$reg]) &&
$data[7] < (time()*1000)
) {
/* it only adds this array, either when there is none */
$array[$reg] = array(
"stop" => $data[1],
"stopID" => $data[2],
"route" => $data[3],
"direction" => $data[4],
"dest" => $data[5],
"fleet" => "00000",
"time" => $data[7],
"when" => date("H:i", $data[7] / 1000)
);
}
}
/* removing up the Java timestamps */
foreach($array as $data) {unset($data['time']);}
die('<pre>'.print_r($array, true).'</pre>');
答案 1 :(得分:0)
下面的函数将根据最低的“时间”值返回最近的车辆。
function nearestVehicle($vehicles){
function cmp($a,$b){
if($a['when']>$b['when']){
return 1;
}
}
usort($vehicles,"cmp");
return $vehicles[0];
}
print_r(nearestVehicle($array));