Json使用PHP将数组嵌套到Mysql中

时间:2018-10-06 10:38:16

标签: php json mysqli

大家好

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

注意:未定义的索引:第24行的C:\ xampp \ htdocs \ working-scripts-jason \ jsontest9.php中的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
  }
]

2 个答案:

答案 0 :(得分:1)

sites是一个数组,因此您需要遍历它以获取数据,或者仅在有一个数组的情况下才使用它:

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

答案 1 :(得分:0)

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>';