注意:未定义的索引:第24行的C:\ xampp \ htdocs \ working-scripts-jason \ jsontest9.php中的site_nice
注意:未定义的索引:第25行的C:\ xampp \ htdocs \ working-scripts-jason \ jsontest9.php中的赔率
<?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
$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);
?>
[
{
"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
}
]
答案 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>';