使用 php 从 json 获取特定值
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'];
不幸的是我只得到值最后得分的球队
$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);
}
我试过了,结果是奇数
遍历投注数组,寻找您感兴趣的投注 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'];
}
}
}
我只需要从一个键中获取特定的值,例如:
我需要从
获取"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'];
不幸的是我只得到值最后得分的球队
$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);
}
我试过了,结果是奇数
遍历投注数组,寻找您感兴趣的投注 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'];
}
}
}