如何解析PHP中的Google Directions API Json,特别是嵌套的每个“步骤”和“腿”的距离值和持续时间值?

问题描述 投票:0回答:1

json响应可以相互嵌套任何数量的Distance-Values,Duration-Values,Steps和Legs。我需要确保变量中的“距离”和“持续时间”原始配对,以便以后进行数学运算。请注意,在我不希望收集的步骤中,文件还具有未嵌套的距离值和持续时间值。我是新手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 \u003cb\u003enorthwest\u003c/b\u003e toward \u003cb\u003eJericho Turnpike\u003c/b\u003e",
                     "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 \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eJericho Turnpike\u003c/b\u003e",
                     "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 \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eS Marginal Rd\u003c/b\u003e",
                     "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 \u003cb\u003eInterstate 495 E\u003c/b\u003e ramp on the \u003cb\u003eleft\u003c/b\u003e to \u003cb\u003eRiverhead\u003c/b\u003e",
                     "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 \u003cb\u003eI-495 E\u003c/b\u003e",
                     "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 \u003cb\u003e42\u003c/b\u003e for \u003cb\u003eBroadway\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNorthern Pkwy E\u003c/b\u003e toward \u003cb\u003eHauppauge\u003c/b\u003e",
                     "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 \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
                     "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 \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
                     "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 \u003cb\u003eNY-347\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eNY-454 E\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNY-347 E\u003c/b\u003e, follow signs for \u003cb\u003ePort Jefferson\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Carvel (on the right)\u003c/div\u003e",
                     "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 \u003cb\u003eSmithtown Bypass\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eStony Brook Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
                     "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 \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eStony Brook Rd\u003c/b\u003e toward \u003cb\u003eDevelopment Drive\u003c/b\u003e",
                     "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 \u003cb\u003eright\u003c/b\u003e onto \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
                     "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 \u003cb\u003eNY-347 W\u003c/b\u003e/\u003cwbr/\u003e\u003cb\u003eSmithtown Bypass\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e to continue on \u003cb\u003eNorthern State Pkwy\u003c/b\u003e",
                     "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 \u003cb\u003e43\u003c/b\u003e for \u003cb\u003eSuffolk County 4\u003c/b\u003e toward \u003cb\u003eCommack\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e at the fork, follow signs for \u003cb\u003eCounty Road 4 N\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eCommack Rd\u003c/b\u003e",
                     "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 \u003cb\u003eleft\u003c/b\u003e onto \u003cb\u003eHauppauge Rd\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003eDestination will be on the right\u003c/div\u003e",
                     "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"
}

php json api parsing directions
1个回答
0
投票

我通过使用带有多个“ if”测试的多个嵌套“ foreach”循环来解决这一问题。我敢肯定这不是最优雅的,但它可以工作。键(双关语的意图)是跟踪每个嵌套的foreach循环的所有索引/键。

© www.soinside.com 2019 - 2024. All rights reserved.