![](/img/trans.png)
[英]How to get A Specifc Key => value pair from an assosative array in Php
[英]get specifc value from json with php
我只需要從一個鍵中獲取特定值,例如:
我"odd": "6.25"
從
"name": "Team To Score Last"->"value": "No goal"
和奇數
這是我的 json
"response": [
{
"league": {},
"fixture": {},
"update": "2020-05-15T09:49:32+00:00",
"bookmakers": [
{
"id": 6,
"name": "Bwin",
"bets": [
{}, {}, {}, {}, {}, {}, {},
{}, {}, {}, {},
{
"id": 15,
"name": "Team To Score Last",
"values": [
{
"value": "No goal",
"odd": "6.25"
}
我試過了
$odds =json_decode($responseodds, true);
$value=$odds['response'][0]['bookmakers'][0]['bets'][0]['name'];
不幸的是,我只得到了最后一個得分的團隊
遍歷 bets 數組,並查找您感興趣的投注name
。
foreach ( $odds['response'][0]['bookmakers'][0]['bets'] as $bet){
if ( $bet['name'] == "Team To Score Last") {
// this is the one
echo 'The odds were ' . $bet['values'][0]['odd'];
}
}
如果你想為所有博彩公司提供這些賠率
foreach ( $odds['response'][0]['bookmakers'] as $bookie){
foreach ( $bookie as $bet){
if ( $bet['name'] == "Team To Score Last") {
// this is the one
echo 'The bookie ' . $bookie['name'] . 'has the odds ' . $bet['values'][0]['odd'];
}
}
}
<?php
if (isset($_POST["submit"])){
include 'config.php';
$name = mysql_real_escape_string($conn, $_POST["name"]);
$email = mysql_real_escape_string($conn, $_POST["email"]);
$password = mysql_real_escape_string($conn, $_POST["password"]);
$cpassword = mysql_real_escape_string($conn, $_POST["cpassword"]);
$roles = mysql_real_escape_string($conn, $_POST["roles"]);
$roles_used = 0;
if (mysql_num_rows(mysqli_query($conn, "SELECT * FROM users WHERE email='{$email}'")) > 0){
echo "<script> alert('{$email} - This email is already used.');</script>";
} else {
if ($password === $cpassword){
$sql = "INSERT INTO users (name, email, password, roles, roles_used) VALUES ('{$name}', '{$email}', '{$password}', '{$roles}', '{$roles_used}')";
$result = mysql_query($conn, $sql);
if ($result){
header("Location: login.html");
}else{
echo "<script>Error: ".$sql.mysql_error($conn)." </script>";
}
}else{
echo "<script>alert('Passwords do not match.');</script>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta https-equiv="X-UA-Compatible" content="IE-edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> MySugar Diabetes Management </title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="main">
<div class="navbar">
<h2 class="logoo"> MY SUGAR : Diabetes Management System </h2>
<div class="wrapper">
<h2 class="title"> Register Account </h2>
<form class="form" action="" method="post">
<div class="input-field">
<label for="name" class="input-label"> Full Name</label>
<input type="name" id="name" name="name" class="input" placeholder="Enter your full name" required>
</div>
<div class="input-field">
<label for="email" class="input-label"> Email</label>
<input type="email" name="email" id="email" class="input" placeholder="Enter your Email" required>
</div>
<div class="input-field">
<label for="password" class="input-label"> Password</label>
<input id="password" type="password" class="input" name="password" placeholder="Enter your Password" required>
</div>
<div class="input-field">
<label for="cpassword" class="input-label">Confirm Password</label>
<input id="password" name="cpassword" type="password" class="input" placeholder="Confirm your Password" required>
</div>
<div class="input-field">
<label for="limit" class="input-label"> Select role</label>
<select id="limit" name="roles" class="input" required>
<option value="1"> Admin </option>
<option value="2"> Patient </option>
<option value="3"> Doctor </option>
</select>
</div>
<button name="submit" class="btn"> Sign Up </button>
<p> Already have an account? <a href="login.html"> Log in </a></p>
</div>
</form>
</body>
</html>
我正在嘗試運行此代碼,但在本地主機上它只是作為代碼行返回。 可能是什么問題?
$json = '{
"response": [
{
"league": {},
"fixture": {},
"update": "2020-05-15T09:49:32+00:00",
"bookmakers": [
{
"id": 6,
"name": "Bwin",
"bets": [
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{},
{
"id": 15,
"name": "Team To Score Last",
"values": [
{
"value": "No goal",
"odd": "6.25"
}
]
}
]
}
]
}
]
}
';
$data = json_decode($json, true);
echo "<pre>";
var_dump($data);
echo "<pre>";
$bets = $data['response'][0]['bookmakers'][0]['bets'];
foreach ($bets as $item) {
if (empty($item)) {
continue;
}
$odd = $item['values'][0]['odd'];
var_dump($item);
var_dump($odd);
}
我試過這個,我有奇數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.