【问题标题】:Json nested Array into Mysql with PHP使用PHP将Json嵌套数组到Mysql中
【发布时间】:2018-10-06 10:38:16
【问题描述】:

大家好

以下代码适用于 Json 脚本的第一部分,但是当我尝试解析整个 Json 脚本时,出现以下错误...

注意:未定义索引:C:\xampp\htdocs\working-scripts-jason\jsontest9.php 第 24 行中的 site_nice

注意:未定义索引:第 25 行 C:\xampp\htdocs\working-scripts-jason\jsontest9.php 中的赔率

工作 PHP 代码如下...解析到 MySQL BD

<?php

    $host = "localhost";
    $username = "student";
    $password = "";
    $dbname = "football";
    $con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));

    $st = mysqli_prepare($con, 'INSERT INTO epl_odds(sport_nice, team1, team2, commence_time, home_team) VALUES (?, ?, ?, ?, ?)');

    mysqli_stmt_bind_param($st, 'sssss', $sport_nice, $team1, $team2, $commence_time, $home_team);

    $filename = 'jsontest9.json';
    $json = file_get_contents($filename);

    $data = json_decode($json, true);

    foreach ($data as $row) {
      $sport_nice = $row['sport_nice'];
      $team1 = $row['teams']['0'];
      $team2 = $row['teams']['1'];
      $commence_time = $row['commence_time'];
      $home_team = $row['home_team'];

        mysqli_stmt_execute($st);
    }

    mysqli_close($con);
?>

以下 PHP 代码不工作...我认为这是因为 Json 文件中的嵌套数组,我无法在“站点”进入数据库后获取数据,经过几天的搜索仍然没有乐趣,我有发现了类似的问题,这些问题帮助我登上了舞台,但停留在这一点上......任何帮助表示赞赏......谢谢......

<?php

    $host = "localhost";
    $username = "student";
    $password = "";
    $dbname = "football";
    $con = mysqli_connect($host, $username, $password, $dbname) or die('Error in Connecting: ' . mysqli_error($con));

    $st = mysqli_prepare($con, 'INSERT INTO epl_odds(sport_nice, team1, team2, commence_time, home_team, site_nice, h2h) VALUES (?, ?, ?, ?, ?, ?, ?)');

    mysqli_stmt_bind_param($st, 'sssssss', $sport_nice, $team1, $team2, $commence_time, $home_team, $site_nice, $h2h);

    $filename = 'jsontest9.json';
    $json = file_get_contents($filename);

    $data = json_decode($json, true);

    foreach ($data as $row) {
      $sport_nice = $row['sport_nice'];
      $team1 = $row['teams']['0'];
      $team2 = $row['teams']['1'];
      $commence_time = $row['commence_time'];
      $home_team = $row['home_team'];
      $site_nice = $row['sites']['site_nice'];
      $h2h = $row['sites']['odds']['h2h']['0'];

        mysqli_stmt_execute($st);
    }

    mysqli_close($con);
?>

Json 文件...

[
  {
    "sport_key": "soccer_epl",
    "sport_nice": "EPL",
    "teams": [
      "Brighton and Hove Albion",
      "West Ham United"
    ],
    "commence_time": 1538766000,
    "home_team": "Brighton and Hove Albion",
    "sites": [
      {
        "site_key": "unibet",
        "site_nice": "Unibet",
        "last_update": 1538526493,
        "odds": {
          "h2h": [
            2.55,
            2.9,
            3.2
          ]
        }
      }
    ],
    "sites_count": 9
  }
]

【问题讨论】:

    标签: php json mysqli


    【解决方案1】:

    sites 是一个数组,所以你需要遍历它来获取数据,或者如果只有一个就使用它:

    $site_nice = $row['sites'][0]['site_nice'];

    【讨论】:

      【解决方案2】:

      sport_nice 是对象sites 的数组。而commence_time, home_team 是对象。 teams 是一个数组。所以你应该尝试获取如下数据:

      foreach ($data as $row) {
            $sites = $row->sites;
            $teams = $row->teams;
            $sport_nice = $sites->sport_nice;
            $team1 = $teams[0];
            $team2 = $teams[1];
            $commence_time = $row->commence_time;
            $home_team = $row->home_team;
      
              mysqli_stmt_execute($st);
          }
      

      注意:首先像在foreach循环中一样打印数据$row数据并查看数据格式。

      echo '<pre>';
      print_r($row);
      echo '</pre>';
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 2018-12-29
        • 1970-01-01
        • 2017-08-25
        • 2017-10-29
        • 2017-10-15
        • 2018-09-05
        相关资源
        最近更新 更多