php - 如何遍历这个困难的多维数组并仅添加某些字段以分隔 mysql 数据库的行
问题描述
所以我有一个相当难以处理的数组,我将在下面放置一个链接,但要点是:我正在尝试遍历这个数组并将每个子数组中的某些字段添加到 mysql 数据库中的行中。
我一直在使用准备好的语句,并试图在每个循环中将以下字段添加到我的数据库中。
- 我存储在 $_SESSION['userreference'] 中的唯一引用
- 姓名
- 距离
- 移动时间
- 最大心率
- 开始日期本地
有问题的数组:
array(30) {
[0]=>
array(52) {
["resource_state"]=>
int(2)
["athlete"]=>
array(2) {
["id"]=>
int(2224878)
["resource_state"]=>
int(1)
}
["name"]=>
string(6) "Easy 5"
["distance"]=>
float(5037.7)
["moving_time"]=>
int(1493)
["elapsed_time"]=>
int(1494)
["total_elevation_gain"]=>
float(11.1)
["type"]=>
string(3) "Run"
["workout_type"]=>
int(0)
["id"]=>
int(3862655039)
["external_id"]=>
string(12) "activity.fit"
["upload_id"]=>
int(4133211829)
["start_date"]=>
string(20) "2020-08-04T11:43:46Z"
["start_date_local"]=>
string(20) "2020-08-04T12:43:46Z"
["timezone"]=>
string(25) "(GMT+00:00) Europe/London"
["utc_offset"]=>
float(3600)
["start_latlng"]=>
array(2) {
[0]=>
float(52.67)
[1]=>
float(0.94)
}
["end_latlng"]=>
array(2) {
[0]=>
float(52.67)
[1]=>
float(0.94)
}
["location_city"]=>
NULL
["location_state"]=>
NULL
["location_country"]=>
string(14) "United Kingdom"
["start_latitude"]=>
float(52.67)
["start_longitude"]=>
float(0.94)
["achievement_count"]=>
int(0)
["kudos_count"]=>
int(13)
["comment_count"]=>
int(0)
["athlete_count"]=>
int(1)
["photo_count"]=>
int(0)
["map"]=>
array(3) {
["id"]=>
string(11) "a3862655039"
["summary_polyline"]=>
string(392) "q|n`Ikew"
["resource_state"]=>
int(2)
}
["trainer"]=>
bool(false)
["commute"]=>
bool(false)
["manual"]=>
bool(false)
["private"]=>
bool(false)
["visibility"]=>
string(8) "everyone"
["flagged"]=>
bool(false)
["gear_id"]=>
string(8) "g3671351"
["from_accepted_tag"]=>
bool(false)
["upload_id_str"]=>
string(10) "4133211829"
["average_speed"]=>
float(3.374)
["max_speed"]=>
float(6.6)
["average_cadence"]=>
float(78)
["has_heartrate"]=>
bool(true)
["average_heartrate"]=>
float(166.3)
["max_heartrate"]=>
float(178)
["heartrate_opt_out"]=>
bool(false)
["display_hide_heartrate_option"]=>
bool(true)
["elev_high"]=>
float(53)
["elev_low"]=>
float(46.2)
["pr_count"]=>
int(0)
["total_photo_count"]=>
int(0)
["has_kudoed"]=>
bool(false)
["suffer_score"]=>
float(44)
}
[1]=>
array(50) {
["resource_state"]=>
int(2)
["athlete"]=>
array(2) {
["id"]=>
int(2224878)
["resource_state"]=>
int(1)
}
["name"]=>
string(12) "Morning Walk"
["distance"]=>
float(10848.7)
["moving_time"]=>
int(8095)
["elapsed_time"]=>
int(9257)
["total_elevation_gain"]=>
float(74.5)
["type"]=>
string(4) "Walk"
["id"]=>
int(3847387184)
["external_id"]=>
string(40) "80B66E48-B4F8-414F-8974-F6F157A09E03.fit"
["upload_id"]=>
int(4116320622)
["start_date"]=>
string(20) "2020-08-01T08:32:14Z"
["start_date_local"]=>
string(20) "2020-08-01T09:32:14Z"
["timezone"]=>
string(25) "(GMT+00:00) Europe/London"
["utc_offset"]=>
float(3600)
["start_latlng"]=>
array(2) {
[0]=>
float(53.47)
[1]=>
float(-1.94)
}
["end_latlng"]=>
array(2) {
[0]=>
float(53.48)
[1]=>
float(-1.9)
}
["location_city"]=>
NULL
["location_state"]=>
NULL
["location_country"]=>
string(14) "United Kingdom"
["start_latitude"]=>
float(53.47)
["start_longitude"]=>
float(-1.94)
["achievement_count"]=>
int(0)
["kudos_count"]=>
int(22)
["comment_count"]=>
int(0)
["athlete_count"]=>
int(1)
["photo_count"]=>
int(0)
["map"]=>
array(3) {
["id"]=>
string(11) "a3847387184"
["summary_polyline"]=>
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@"
["resource_state"]=>
int(2)
}
["trainer"]=>
bool(false)
["commute"]=>
bool(false)
["manual"]=>
bool(false)
["private"]=>
bool(false)
["visibility"]=>
string(8) "everyone"
["flagged"]=>
bool(false)
["gear_id"]=>
string(8) "g6163360"
["from_accepted_tag"]=>
bool(false)
["upload_id_str"]=>
string(10) "4116320622"
["average_speed"]=>
float(1.34)
["max_speed"]=>
float(2.7)
["has_heartrate"]=>
bool(true)
["average_heartrate"]=>
float(91.6)
["max_heartrate"]=>
float(118)
["heartrate_opt_out"]=>
bool(false)
["display_hide_heartrate_option"]=>
bool(true)
["elev_high"]=>
float(276.7)
["elev_low"]=>
float(208.7)
["pr_count"]=>
int(0)
["total_photo_count"]=>
int(0)
["has_kudoed"]=>
bool(false)
["suffer_score"]=>
float(15)
}
ETC....
到目前为止我的代码可能都是错误的,所以对任何建议持开放态度!
$response3 = curl_exec($curl3); // cURL request which gives me json
curl_close($curl3);
$runs = json_decode($response3, true); // turning that json into the array
// the for and foreach loop i'm trying to use and insert on each loop. The db is connected further up on this page.
//Completely open that everything below this is wrong, i've been banging my head against a wall trying everything. open to any suggestions
$keys = array_keys($runs);
for($i = 0; $i < count($runs); $i++) {
foreach($runs[$keys[$i]] as $key => $value) {
$stmt = $conn->prepare("INSERT INTO runs (userReferenceRuns, runName, runDate, runDistance, runDuration) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $_SESSION['userReference'], $names, $startdate, $distances, $movingtime);
$stmt->execute();
}
}
解决方案
推荐阅读
- matlab - 在 Matlab 中加速 mex 文件
- mongodb - MongoDB:查找没有子文档符合条件的文档
- javascript - 您如何使用 Javascript 更改 html 表格的背景
- reactjs - 观察者 mobx-react 似乎在 React 中不起作用
- docker - apache启动后运行drush命令
- javascript - D3 链接模式
- python - 如何在没有 for 循环的情况下计算许多图像的质心?
- python - 递归方法调用可以在 Python 3 中共享字典类型的变量吗
- memory-management - LFR/LRU 是如何实现的?
- java - 一键更改多个 JPanel 的颜色