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

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

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

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)
  }

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

$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();
    }
}

暂无答案!

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

相关问题