php—如何在这个复杂的多维数组中循环,并只向mysql数据库的各行中添加特定字段

ef1yzkbh  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(297)

所以我有一个很难处理的数组,我将在下面链接到它,但要点是:我试图循环遍历这个数组,并将每个子数组中的某些字段添加到mysql数据库中的行中。
我一直在使用prepared语句,并尝试将以下字段添加到每个循环的db中。
我存储在$\u session['userreference']中的唯一引用
名称
距离
移动时间
最大心率
开始日期\u本地
有问题的数组:

  1. array(30) {
  2. [0]=>
  3. array(52) {
  4. ["resource_state"]=>
  5. int(2)
  6. ["athlete"]=>
  7. array(2) {
  8. ["id"]=>
  9. int(2224878)
  10. ["resource_state"]=>
  11. int(1)
  12. }
  13. ["name"]=>
  14. string(6) "Easy 5"
  15. ["distance"]=>
  16. float(5037.7)
  17. ["moving_time"]=>
  18. int(1493)
  19. ["elapsed_time"]=>
  20. int(1494)
  21. ["total_elevation_gain"]=>
  22. float(11.1)
  23. ["type"]=>
  24. string(3) "Run"
  25. ["workout_type"]=>
  26. int(0)
  27. ["id"]=>
  28. int(3862655039)
  29. ["external_id"]=>
  30. string(12) "activity.fit"
  31. ["upload_id"]=>
  32. int(4133211829)
  33. ["start_date"]=>
  34. string(20) "2020-08-04T11:43:46Z"
  35. ["start_date_local"]=>
  36. string(20) "2020-08-04T12:43:46Z"
  37. ["timezone"]=>
  38. string(25) "(GMT+00:00) Europe/London"
  39. ["utc_offset"]=>
  40. float(3600)
  41. ["start_latlng"]=>
  42. array(2) {
  43. [0]=>
  44. float(52.67)
  45. [1]=>
  46. float(0.94)
  47. }
  48. ["end_latlng"]=>
  49. array(2) {
  50. [0]=>
  51. float(52.67)
  52. [1]=>
  53. float(0.94)
  54. }
  55. ["location_city"]=>
  56. NULL
  57. ["location_state"]=>
  58. NULL
  59. ["location_country"]=>
  60. string(14) "United Kingdom"
  61. ["start_latitude"]=>
  62. float(52.67)
  63. ["start_longitude"]=>
  64. float(0.94)
  65. ["achievement_count"]=>
  66. int(0)
  67. ["kudos_count"]=>
  68. int(13)
  69. ["comment_count"]=>
  70. int(0)
  71. ["athlete_count"]=>
  72. int(1)
  73. ["photo_count"]=>
  74. int(0)
  75. ["map"]=>
  76. array(3) {
  77. ["id"]=>
  78. string(11) "a3862655039"
  79. ["summary_polyline"]=>
  80. string(392) "q|n`Ikew"
  81. ["resource_state"]=>
  82. int(2)
  83. }
  84. ["trainer"]=>
  85. bool(false)
  86. ["commute"]=>
  87. bool(false)
  88. ["manual"]=>
  89. bool(false)
  90. ["private"]=>
  91. bool(false)
  92. ["visibility"]=>
  93. string(8) "everyone"
  94. ["flagged"]=>
  95. bool(false)
  96. ["gear_id"]=>
  97. string(8) "g3671351"
  98. ["from_accepted_tag"]=>
  99. bool(false)
  100. ["upload_id_str"]=>
  101. string(10) "4133211829"
  102. ["average_speed"]=>
  103. float(3.374)
  104. ["max_speed"]=>
  105. float(6.6)
  106. ["average_cadence"]=>
  107. float(78)
  108. ["has_heartrate"]=>
  109. bool(true)
  110. ["average_heartrate"]=>
  111. float(166.3)
  112. ["max_heartrate"]=>
  113. float(178)
  114. ["heartrate_opt_out"]=>
  115. bool(false)
  116. ["display_hide_heartrate_option"]=>
  117. bool(true)
  118. ["elev_high"]=>
  119. float(53)
  120. ["elev_low"]=>
  121. float(46.2)
  122. ["pr_count"]=>
  123. int(0)
  124. ["total_photo_count"]=>
  125. int(0)
  126. ["has_kudoed"]=>
  127. bool(false)
  128. ["suffer_score"]=>
  129. float(44)
  130. }
  131. [1]=>
  132. array(50) {
  133. ["resource_state"]=>
  134. int(2)
  135. ["athlete"]=>
  136. array(2) {
  137. ["id"]=>
  138. int(2224878)
  139. ["resource_state"]=>
  140. int(1)
  141. }
  142. ["name"]=>
  143. string(12) "Morning Walk"
  144. ["distance"]=>
  145. float(10848.7)
  146. ["moving_time"]=>
  147. int(8095)
  148. ["elapsed_time"]=>
  149. int(9257)
  150. ["total_elevation_gain"]=>
  151. float(74.5)
  152. ["type"]=>
  153. string(4) "Walk"
  154. ["id"]=>
  155. int(3847387184)
  156. ["external_id"]=>
  157. string(40) "80B66E48-B4F8-414F-8974-F6F157A09E03.fit"
  158. ["upload_id"]=>
  159. int(4116320622)
  160. ["start_date"]=>
  161. string(20) "2020-08-01T08:32:14Z"
  162. ["start_date_local"]=>
  163. string(20) "2020-08-01T09:32:14Z"
  164. ["timezone"]=>
  165. string(25) "(GMT+00:00) Europe/London"
  166. ["utc_offset"]=>
  167. float(3600)
  168. ["start_latlng"]=>
  169. array(2) {
  170. [0]=>
  171. float(53.47)
  172. [1]=>
  173. float(-1.94)
  174. }
  175. ["end_latlng"]=>
  176. array(2) {
  177. [0]=>
  178. float(53.48)
  179. [1]=>
  180. float(-1.9)
  181. }
  182. ["location_city"]=>
  183. NULL
  184. ["location_state"]=>
  185. NULL
  186. ["location_country"]=>
  187. string(14) "United Kingdom"
  188. ["start_latitude"]=>
  189. float(53.47)
  190. ["start_longitude"]=>
  191. float(-1.94)
  192. ["achievement_count"]=>
  193. int(0)
  194. ["kudos_count"]=>
  195. int(22)
  196. ["comment_count"]=>
  197. int(0)
  198. ["athlete_count"]=>
  199. int(1)
  200. ["photo_count"]=>
  201. int(0)
  202. ["map"]=>
  203. array(3) {
  204. ["id"]=>
  205. string(11) "a3847387184"
  206. ["summary_polyline"]=>
  207. string(846) "{rjeIlpyJw}@sxFNDFEDUFeAEk@Uo@COBGFE`@AWyAc@eBAWFUIAOQuAaCu@w@]g@_BqBOMIc@@MFOMNG?GCc@i@}AwA{A}AwAkCEQ@SMLMa@?AD?BSLQ?GMPEb@EBGAMO[k@s@cBeBmDyBcHgB_H_@uBo@wCOoC@QFQE?GAOSYg@Oc@g@gCs@cDUy@g@uAm@kAOg@kAqB_@_Ak@gAGYg@kA[eBY_Ay@aBe@u@]c@i@mB[aAQa@]g@]}@IMIg@W_AQoA_@yC[qD_@sFAwC@uKPuBDwB^mHNeBJsBD}DEgEDm@CwAMsBEa@Qy@_@mAOcBMa@L^\dC`@fAKmAGgA[qCYcEQuAYeEYgBWiCQwAUqCi@eFYcDsAgL]mEA}AMkCB{@@yEEkA?iAIoB@_DMyIAwFBsBC{C@kAEoCa@sIk@eISuBUqDg@{FEeAOeA[sDMiAIwBQgAKaBEOICDWAUS}AIqA?YFJ@HDM?eAFe@d@y@Za@Ra@FEJB@BQA{@fAWd@Mb@MpAELDDZpCT`EX|BHpBNjBR`BVnDPrAj@fG`AdN\jJHrA?^I|@Aj@FjB?vFB~A?xBFrDA~CDxBD|DFdBCvCH|Ej@nHR`B\jCRzBTjBRzBZtCb@|EXlCJvAn@|Fv@vIb@pFTrGB`EG|EExAi@fI]|HE`BClIN`FXxEb@rEp@hEd@rBj@z@Vh@fAxDfA~A|@fBJ^l@nC`AhCrAzBp@xAbA`Cl@|BXvAb@vAVhANf@Zt@Nl@Fd@@lADn@^`BTtAh@fCjDhLjAjDp@bBnBjD|AbC|AnBfAhAl@v@xB~BxBhCrAvBf@dABRERg@f@M^MdAQdAItA@b@h@z@Ll@"
  208. ["resource_state"]=>
  209. int(2)
  210. }
  211. ["trainer"]=>
  212. bool(false)
  213. ["commute"]=>
  214. bool(false)
  215. ["manual"]=>
  216. bool(false)
  217. ["private"]=>
  218. bool(false)
  219. ["visibility"]=>
  220. string(8) "everyone"
  221. ["flagged"]=>
  222. bool(false)
  223. ["gear_id"]=>
  224. string(8) "g6163360"
  225. ["from_accepted_tag"]=>
  226. bool(false)
  227. ["upload_id_str"]=>
  228. string(10) "4116320622"
  229. ["average_speed"]=>
  230. float(1.34)
  231. ["max_speed"]=>
  232. float(2.7)
  233. ["has_heartrate"]=>
  234. bool(true)
  235. ["average_heartrate"]=>
  236. float(91.6)
  237. ["max_heartrate"]=>
  238. float(118)
  239. ["heartrate_opt_out"]=>
  240. bool(false)
  241. ["display_hide_heartrate_option"]=>
  242. bool(true)
  243. ["elev_high"]=>
  244. float(276.7)
  245. ["elev_low"]=>
  246. float(208.7)
  247. ["pr_count"]=>
  248. int(0)
  249. ["total_photo_count"]=>
  250. int(0)
  251. ["has_kudoed"]=>
  252. bool(false)
  253. ["suffer_score"]=>
  254. float(15)
  255. }

等。。。。
代码我到目前为止,这可能是所有的错误,所以开放的任何建议!

  1. $response3 = curl_exec($curl3); // cURL request which gives me json
  2. curl_close($curl3);
  3. $runs = json_decode($response3, true); // turning that json into the array
  4. // the for and foreach loop i'm trying to use and insert on each loop. The db is connected further up on this page.
  5. //Completely open that everything below this is wrong, i've been banging my head against a wall trying everything. open to any suggestions
  6. $keys = array_keys($runs);
  7. for($i = 0; $i < count($runs); $i++) {
  8. foreach($runs[$keys[$i]] as $key => $value) {
  9. $stmt = $conn->prepare("INSERT INTO runs (userReferenceRuns, runName, runDate, runDistance, runDuration) VALUES (?, ?, ?, ?, ?)");
  10. $stmt->bind_param("sssss", $_SESSION['userReference'], $names, $startdate, $distances, $movingtime);
  11. $stmt->execute();
  12. }
  13. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题