如何在PHP中解析Google Directions API Json,特别是每个"Step"和"Leg"的距离值和持续时间值,它们都是嵌套的?



json 响应可以嵌套任意数量的距离值、持续时间值、步数和腿。我需要确保变量中距离和持续时间的原始配对,以便以后进行数学运算。 请注意,该文件还具有距离值和持续时间值,未嵌套在我不感兴趣的步骤中。 我是一个新手PHP程序员 - 请温柔一点。谢谢。

这是一个示例 json 文件响应,抱歉它太长了。

{
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJq0F7QdOGwokRtGWVrOp28h0",
"types" : [ "premise" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJvzh05jM_6IkRhcy9aqUcTHM",
"types" : [ "establishment", "point_of_interest", "university" ]
},
{
"geocoder_status" : "OK",
"partial_match" : true,
"place_id" : "ChIJ28a23cMv6IkRLzOqZ0h6vwo",
"types" : [ "premise" ]
}
],
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 40.9003613,
"lng" : -73.11792029999999
},
"southwest" : {
"lat" : 40.7791055,
"lng" : -73.5611243
}
},
"copyrights" : "Map data ©2020 Google",
"legs" : [
{
"distance" : {
"text" : "28.7 mi",
"value" : 46179
},
"duration" : {
"text" : "40 mins",
"value" : 2424
},
"end_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"start_address" : "99 Jericho Turnpike, Westbury, NY 11590, USA",
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"steps" : [
{
"distance" : {
"text" : "128 ft",
"value" : 39
},
"duration" : {
"text" : "1 min",
"value" : 14
},
"end_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"html_instructions" : "Head u003cbu003enorthwestu003c/bu003e toward u003cbu003eJericho Turnpikeu003c/bu003e",
"polyline" : {
"points" : "mt{wFnj~_M{@n@"
},
"start_location" : {
"lat" : 40.7791055,
"lng" : -73.56088389999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 629
},
"duration" : {
"text" : "3 mins",
"value" : 158
},
"end_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"html_instructions" : "Turn u003cbu003erightu003c/bu003e onto u003cbu003eJericho Turnpikeu003c/bu003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "iv{wF~k~_Me@oAg@wAQe@M_@]_Ag@sAQe@g@wA[s@K[a@eAWu@Qs@I[m@gCi@{BAEk@{Bg@yB"
},
"start_location" : {
"lat" : 40.7794066,
"lng" : -73.5611243
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 251
},
"duration" : {
"text" : "1 min",
"value" : 15
},
"end_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"html_instructions" : "Slight u003cbu003erightu003c/bu003e onto u003cbu003eS Marginal Rdu003c/bu003e",
"maneuver" : "turn-slight-right",
"polyline" : {
"points" : "cg|wFbc}_MEe@AGAECKIc@I_@IYEY[mBKy@Ik@Ge@E_@EYAYCYAY"
},
"start_location" : {
"lat" : 40.7821022,
"lng" : -73.5545772
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 218
},
"duration" : {
"text" : "1 min",
"value" : 13
},
"end_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"html_instructions" : "Take the u003cbu003eInterstate 495 Eu003c/bu003e ramp on the u003cbu003eleftu003c/bu003e to u003cbu003eRiverheadu003c/bu003e",
"maneuver" : "ramp-left",
"polyline" : {
"points" : "ck|wFhq|_MIYAEAICMASAMKeAKuAC]Cm@AAAYCi@Cs@AK?GAIAGACCEEI"
},
"start_location" : {
"lat" : 40.7827402,
"lng" : -73.55172809999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.3 mi",
"value" : 2154
},
"duration" : {
"text" : "1 min",
"value" : 83
},
"end_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"html_instructions" : "Merge onto u003cbu003eI-495 Eu003c/bu003e",
"maneuver" : "merge",
"polyline" : {
"points" : "wm|wFra|_MW{MWeIEq@]eIWeFOcCG{@UmD_@cFU}BQaBQaBQeBuAsMc@iDGe@Gi@c@}Cg@mDg@cD[mBc@kCYgB]mBa@cC_@kBi@eC[kA[mAa@yAOg@Wy@Qi@]eA"
},
"start_location" : {
"lat" : 40.7831579,
"lng" : -73.54922080000001
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.4 mi",
"value" : 679
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"html_instructions" : "Take exit u003cbu003e42u003c/bu003e for u003cbu003eBroadwayu003c/bu003e/u003cwbr/u003eu003cbu003eNorthern Pkwy Eu003c/bu003e toward u003cbu003eHauppaugeu003c/bu003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "qj}wFngw_MAa@AGK[OIOGMEMOC"
},
"start_location" : {
"lat" : 40.7877687,
"lng" : -73.5245628
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "16.0 mi",
"value" : 25810
},
"duration" : {
"text" : "16 mins",
"value" : 945
},
"end_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"html_instructions" : "Merge onto u003cbu003eNorthern State Pkwyu003c/bu003e",
"maneuver" : "merge",
"polyline" : {
"points" : "g|}wFh_v_Me@wAK[a@]@[@[@]@YBWBU@W@CB]DYD_@NaB"
},
"start_location" : {
"lat" : 40.79059609999999,
"lng" : -73.5181288
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.3 mi",
"value" : 463
},
"duration" : {
"text" : "1 min",
"value" : 18
},
"end_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"html_instructions" : "Merge onto u003cbu003eNY-347u003c/bu003e/u003cwbr/u003eu003cbu003eNY-454 Eu003c/bu003e",
"maneuver" : "merge",
"polyline" : {
"points" : "coexF`{a~LBQBOF_@H_@F[Ha@H_@Ji@Je@H_@XmA\oAVkA~CwM"
},
"start_location" : {
"lat" : 40.829462,
"lng" : -73.25121489999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2820
},
"duration" : {
"text" : "3 mins",
"value" : 163
},
"end_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"html_instructions" : "Continue straight to stay on u003cbu003eNY-347u003c/bu003e/u003cwbr/u003eu003cbu003eNY-454 Eu003c/bu003e",
"maneuver" : "straight",
"polyline" : {
"points" : "{dexFn{`~L^_Bf@aCLi@b@{BlAyF^kB`@kBFWTeAd@gC`@gCN{@l@cF?IJcAPcCNoB`@eFTwCFcAXkD@[Dq@|@aMB[XiDVkDDw@NiCJyCHgD@gA@qA?c@?uAA[AqA?WC}@K{DEqAGmCAc@CqAGkBMcFA_@E_ACaACaAIoBA]A}A?qB?Y@oDH_EDkBVsDJsA"
},
"start_location" : {
"lat" : 40.8278228,
"lng" : -73.2461648
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "6.2 mi",
"value" : 9936
},
"duration" : {
"text" : "11 mins",
"value" : 689
},
"end_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"html_instructions" : "Keep u003cbu003eleftu003c/bu003e to continue on u003cbu003eNY-347 Eu003c/bu003e, follow signs for u003cbu003ePort Jeffersonu003c/bu003eu003cdiv style="font-size:0.9em"u003ePass by Carvel (on the right)u003c/divu003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_odxFvnz}LFaD@o@Iw[q@yAeD_AqBk@oA{@mBoBmEc@cAoDwHQ_@"
},
"start_location" : {
"lat" : 40.8243168,
"lng" : -73.21340239999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.2 mi",
"value" : 281
},
"duration" : {
"text" : "1 min",
"value" : 34
},
"end_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"html_instructions" : "Continue onto u003cbu003eSmithtown Bypassu003c/bu003e",
"polyline" : {
"points" : "ijnxFdkh}LcAuBQ]eCyEo@wAg@iA"
},
"start_location" : {
"lat" : 40.8747687,
"lng" : -73.12067499999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "1.8 mi",
"value" : 2899
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"html_instructions" : "Turn u003cbu003eleftu003c/bu003e onto u003cbu003eStony Brook Rdu003c/bu003eu003cdiv style="font-size:0.9em"u003eDestination will be on the rightu003c/divu003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "}snxFtzg}LIU]TmBx@WJo@XiAd@IDqB|@sAj@sF`CoFzBaHvCmF|BaDrAw@`@wDbBiAt@gBpAMNo@d@_@TiBpAcAt@cCbB]VUN}BxAUNe@VoAp@aCnAcDdBiAl@}Ax@MHi@Tc@PkA\wCv@yA`@{Ab@qAZqAVkCf@_BZWFiB\uAXiB`@e@JeB\[HMBSFA?WJ_@Tc@ZML"
},
"start_location" : {
"lat" : 40.8763118,
"lng" : -73.1180346
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
},
{
"distance" : {
"text" : "13.7 mi",
"value" : 21984
},
"duration" : {
"text" : "25 mins",
"value" : 1473
},
"end_address" : "18 Hauppauge Rd, Commack, NY 11725, USA",
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"start_address" : "100 Nicolls Rd, Stony Brook, NY 11794, USA",
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"steps" : [
{
"distance" : {
"text" : "1.8 mi",
"value" : 2870
},
"duration" : {
"text" : "4 mins",
"value" : 258
},
"end_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"html_instructions" : "Head u003cbu003esoutheastu003c/bu003e on u003cbu003eStony Brook Rdu003c/bu003e toward u003cbu003eDevelopment Driveu003c/bu003e",
"polyline" : {
"points" : "gjsxFfij}LLMb@[^UVK@?RGLCZIdB]d@KhBa@tAYhB]VG~A[jCg@pAWpA[zAc@xAa@vCw@jA]b@Qh@ULI|Ay@hAm@bDeB`CoAnAq@d@WTO|ByATO\WbCcBbAu@hBqA^Un@e@VK`BiAjAq@tDaBx@_@nAg@pAi@lF}BpEmBpAk@nF{BrF_C`Bs@jBy@pAi@`Ac@nB{@"
},
"start_location" : {
"lat" : 40.9003613,
"lng" : -73.1305969
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "3.6 mi",
"value" : 5810
},
"duration" : {
"text" : "8 mins",
"value" : 456
},
"end_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"html_instructions" : "Turn u003cbu003erightu003c/bu003e onto u003cbu003eNY-347 Wu003c/bu003e/u003cwbr/u003eu003cbu003eSmithtown Bypassu003c/bu003e",
"maneuver" : "turn-right",
"polyline" : {
"points" : "{tnxFh{g}LhD~Gp@tCh@xA~@|B"
},
"start_location" : {
"lat" : 40.87645940000001,
"lng" : -73.1181333
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "4.6 mi",
"value" : 7390
},
"duration" : {
"text" : "7 mins",
"value" : 430
},
"end_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"html_instructions" : "Continue straight to stay on u003cbu003eNY-347 Wu003c/bu003e/u003cwbr/u003eu003cbu003eSmithtown Bypassu003c/bu003e",
"maneuver" : "straight",
"polyline" : {
"points" : "u|hxFzhr}Lj@fA|EVQv@sAjGs@|CS~@WbA"
},
"start_location" : {
"lat" : 40.8469852,
"lng" : -73.17149669999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "2.9 mi",
"value" : 4593
},
"duration" : {
"text" : "3 mins",
"value" : 177
},
"end_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"html_instructions" : "Keep u003cbu003eleftu003c/bu003e to continue on u003cbu003eNorthern State Pkwyu003c/bu003e",
"maneuver" : "keep-left",
"polyline" : {
"points" : "_kexF`ha~L[pE`BI`D"
},
"start_location" : {
"lat" : 40.8288007,
"lng" : -73.2481703
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.1 mi",
"value" : 176
},
"duration" : {
"text" : "1 min",
"value" : 21
},
"end_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"html_instructions" : "Take exit u003cbu003e43u003c/bu003e for u003cbu003eSuffolk County 4u003c/bu003e toward u003cbu003eCommacku003c/bu003e",
"maneuver" : "ramp-right",
"polyline" : {
"points" : "skcxFfnj~LMb@ADADADABABCBABCBGDABC@GBC@E?C@A?A?C?C?C?AAE?CACAECECGECCAECCACCICICOCUOkAIe@"
},
"start_location" : {
"lat" : 40.8186598,
"lng" : -73.2952425
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "299 ft",
"value" : 91
},
"duration" : {
"text" : "1 min",
"value" : 19
},
"end_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"html_instructions" : "Keep u003cbu003eleftu003c/bu003e at the fork, follow signs for u003cbu003eCounty Road 4 Nu003c/bu003e",
"maneuver" : "fork-left",
"polyline" : {
"points" : "cqcxFrjj~LQ{AOyAC["
},
"start_location" : {
"lat" : 40.8195393,
"lng" : -73.29465789999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "0.6 mi",
"value" : 997
},
"duration" : {
"text" : "2 mins",
"value" : 92
},
"end_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"html_instructions" : "Turn u003cbu003eleftu003c/bu003e onto u003cbu003eCommack Rdu003c/bu003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "ircxF`dj~L?IAQKA{@CE?cAA_AC_AC_@CqEMw@Cw@A_BAYFWAQ?cCGsDK{FOsCIs@A[@Q?gA@E@k@BU@U@"
},
"start_location" : {
"lat" : 40.8197276,
"lng" : -73.29361089999999
},
"travel_mode" : "DRIVING"
},
{
"distance" : {
"text" : "187 ft",
"value" : 57
},
"duration" : {
"text" : "1 min",
"value" : 20
},
"end_location" : {
"lat" : 40.8286064,
"lng" : -73.2937987
},
"html_instructions" : "Turn u003cbu003eleftu003c/bu003e onto u003cbu003eHauppauge Rdu003c/bu003eu003cdiv style="font-size:0.9em"u003eDestination will be on the rightu003c/divu003e",
"maneuver" : "turn-left",
"polyline" : {
"points" : "siexF~`j~LAp@An@Ad@"
},
"start_location" : {
"lat" : 40.82857569999999,
"lng" : -73.29312469999999
},
"travel_mode" : "DRIVING"
}
],
"traffic_speed_entry" : [],
"via_waypoint" : []
}
],
"overview_polyline" : {
"points" : "mt{wFnj~_M{@n@e@oAy@tA"
},
"summary" : "Northern State Pkwy and NY-347",
"warnings" : [],
"waypoint_order" : [ 0 ]
}
],
"status" : "OK"
}

我通过使用多个嵌套的"foreach"循环和几个"if"测试来解决这个问题。 我敢肯定这不是最优雅的,但它有效。 键(双关语(是跟踪每个嵌套的foreach循环的所有索引/键。

最新更新