0

I'm new to json and will appreciate help with this. I have a json string looking like:

{"success":1,"pager":{"page":1,"per_page":250,"total":1813},"results":[{"Id":202322021,"SportId":4,"SportName":"Football","RegionId":31,"RegionName":"Turkey","LeagueId":102729,"LeagueName":"Turkiye Kupasi","BetRadarId":29664196,"our_event_id":4157451,"IsPreMatch":true,"Date":"2021-10-27T17:45:00Z","updated_at":"1635354806","HomeTeam":"Kayserispor","HomeTeamId":230391,"AwayTeam":"Artvin Hopaspor","AwayTeamId":228914,"Markets":[],"optionMarkets":[{"properties":[],"comboPrevention":"NoFixtureCombo","templateCategory":{"category":"Gridable","id":0},"status":"Visible","isMain":true,"grouping":{"gridGroups":["ls8o6ll2v"],"detailed":[{"name":"Match Result","marketTabId":1,"marketHelpPath":"Football\/Match Result (Regular Time)","index":7930,"displayType":"Regular","group":0}],"parameters":{"marketType":"ThreeWay","period":"RegularTime","happening":"Goal"}},"id":29622716,"name":{"value":"Match Result","sign":"9Vlc5w=="},"minCombo":1,"options":[{"name":{"value":"Kayserispor","sign":"tyg4wg=="},"id":96896088,"sourceName":{"value":"1"},"status":"Visible","price":{"id":145532907,"odds":1.055,"americanOdds":-2000,"denominator":18,"numerator":1}},{"price":{"id":145532911,"odds":11,"americanOdds":1000,"numerator":10,"denominator":1},"status":"Visible","name":{"value":"X","sign":"JHhicw=="},"id":96896089},{"price":{"id":145532915,"odds":31,"americanOdds":3000,"numerator":30,"denominator":1},"status":"Visible","sourceName":{"value":"2"},"id":96896090,"name":{"value":"Artvin Hopaspor","sign":"2ruL4g=="}}],"parameters":[{"key":"Happening","value":"Goal","type":"String"},{"key":"MarketType","value":"3way","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}]},{"minCombo":1,"id":29622721,"name":{"value":"Double Chance","sign":"WRDxhg=="},"parameters":[{"key":"Happening","type":"String","value":"Goal"},{"key":"MarketType","value":"DoubleChance","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}],"options":[{"status":"Suspended","price":{"odds":1,"id":145532955},"id":96896100,"name":{"sign":"YrFHYg==","value":"Kayserispor or X"}},{"name":{"value":"X or Artvin Hopaspor","sign":"knPxTA=="},"id":96896101,"status":"Visible","price":{"americanOdds":825,"denominator":4,"numerator":33,"id":145532959,"odds":9.25}},{"price":{"id":145532963,"odds":1.04,"americanOdds":-2500,"numerator":1,"denominator":25},"status":"Visible","name":{"sign":"fwDTwg==","value":"Kayserispor or Artvin Hopaspor"},"id":96896102}],"comboPrevention":"NoFixtureCombo","properties":[],"isMain":false,"status":"Visible","templateCategory":{"id":0,"category":"Gridable"},"grouping":{"parameters":{"period":"RegularTime","marketType":"DoubleChance","happening":"Goal"},"detailed":[{"marketTabId":1,"name":"Double Chance","group":0,"displayType":"Regular","marketHelpPath":"Football\/Double Chance","index":17230}],"gridGroups":["e8vi2hdwz"]}},{"options":[{"price":{"numerator":87,"denominator":100,"americanOdds":-115,"odds":1.87,"id":155223689},"status":"Visible","name":{"sign":"5Rageg==","value":"Over 3,5"},"id":100328998},{"price":{"numerator":5,"denominator":6,"americanOdds":-120,"odds":1.83,"id":155223693},"status":"Visible","id":100328999,"name":{"value":"Under 3,5","sign":"KP8xUw=="}}],"parameters":[{"type":"Decimal","value":"3.5000","key":"DecimalValue"},{"key":"Happening","value":"Goal","type":"String"},{"type":"String","value":"Over\/Under","key":"MarketType"},{"key":"Period","type":"String","value":"RegularTime"}],"name":{"value":"Total Goals","sign":"Y9HR2w=="},"id":30811485,"minCombo":1,"spread":0.04,"grouping":{"parameters":{"period":"RegularTime","marketType":"OverUnder","attr":"3.5000","happening":"Goal","attrType":"Decimal"},"detailed":[{"name":"Over\/Under Total Goals","marketTabId":1,"subIndex":4,"displayType":"OverUnder","index":27330,"group":0},{"subIndex":4,"marketTabId":1,"group":0,"displayType":"OverUnder","index":35130},{"group":2,"displayType":"OverUnder","index":11730,"subIndex":4,"marketTabId":1,"name":"Over\/Under Total Goals"},{"index":19530,"displayType":"OverUnder","group":2,"marketTabId":1,"subIndex":4}],"gridGroups":["tabpo1c9n"]},"balanced":1,"status":"Visible","attr":"3,5","isMain":false,"templateCategory":{"category":"Gridable","id":0},"properties":[],"comboPrevention":"NoFixtureCombo"}]},

I am able to read the straight forward data such as HomeTeam, AwayTeam, Date using the following php code:

  <?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$mk=0;
$json=file_get_contents('https://api.b365api.com/v1/bwin/prematch?token=TOKEN-KEY');
$json_data=json_decode($json,true);

foreach ($json_data['results'] as $key => $value) 
{
if ($value['SportName']=="Football")
    {
        echo "HomeTeam ".$value['HomeTeam']."  ";
        echo "AwayTeam ".$value['AwayTeam']."<br>";
    echo "LeagueName ".$value['LeagueName']."   ".$value['Date']."<br>";
    
    foreach ($value['optionMarkets'] as $keyy => $val)
{
echo "val ".$val;
}

}
}
?>

I will like to read the data from "optionMarkets": to the end. Any help will be appreciated. Thanks.

Balastrong
  • 4,336
  • 2
  • 12
  • 31
Ayo Owoade
  • 27
  • 5
  • Does this answer your question? [How to extract and access data from JSON with PHP?](https://stackoverflow.com/questions/29308898/how-to-extract-and-access-data-from-json-with-php) – ADyson Oct 27 '21 at 18:33
  • How would you like to read it? It's not clear what the expected output is, the output you are seeking. – Robert Oct 27 '21 at 18:52
  • "Robert Rocha....i want the value of every variable. – Ayo Owoade Oct 27 '21 at 20:29
  • @Robert Rocha ...... For each ['results']['id'], i want to extract HomeTeam, AwayTeam, Date, LeagueName, then extract the optionsMarkets Arrays. Thanks for your help. – Ayo Owoade Oct 27 '21 at 21:10

2 Answers2

1

This will give you all fields of your JSON string.
Your JSON string was not valid. This is the valid:

{"success":1,"pager":{"page":1,"per_page":250,"total":1813},"results":[{"Id":202322021,"SportId":4,"SportName":"Football","RegionId":31,"RegionName":"Turkey","LeagueId":102729,"LeagueName":"Turkiye Kupasi","BetRadarId":29664196,"our_event_id":4157451,"IsPreMatch":true,"Date":"2021-10-27T17:45:00Z","updated_at":"1635354806","HomeTeam":"Kayserispor","HomeTeamId":230391,"AwayTeam":"Artvin Hopaspor","AwayTeamId":228914,"Markets":[],"optionMarkets":[{"properties":[],"comboPrevention":"NoFixtureCombo","templateCategory":{"category":"Gridable","id":0},"status":"Visible","isMain":true,"grouping":{"gridGroups":["ls8o6ll2v"],"detailed":[{"name":"Match Result","marketTabId":1,"marketHelpPath":"Football\/Match Result (Regular Time)","index":7930,"displayType":"Regular","group":0}],"parameters":{"marketType":"ThreeWay","period":"RegularTime","happening":"Goal"}},"id":29622716,"name":{"value":"Match Result","sign":"9Vlc5w=="},"minCombo":1,"options":[{"name":{"value":"Kayserispor","sign":"tyg4wg=="},"id":96896088,"sourceName":{"value":"1"},"status":"Visible","price":{"id":145532907,"odds":1.055,"americanOdds":-2000,"denominator":18,"numerator":1}},{"price":{"id":145532911,"odds":11,"americanOdds":1000,"numerator":10,"denominator":1},"status":"Visible","name":{"value":"X","sign":"JHhicw=="},"id":96896089},{"price":{"id":145532915,"odds":31,"americanOdds":3000,"numerator":30,"denominator":1},"status":"Visible","sourceName":{"value":"2"},"id":96896090,"name":{"value":"Artvin Hopaspor","sign":"2ruL4g=="}}],"parameters":[{"key":"Happening","value":"Goal","type":"String"},{"key":"MarketType","value":"3way","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}]},{"minCombo":1,"id":29622721,"name":{"value":"Double Chance","sign":"WRDxhg=="},"parameters":[{"key":"Happening","type":"String","value":"Goal"},{"key":"MarketType","value":"DoubleChance","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}],"options":[{"status":"Suspended","price":{"odds":1,"id":145532955},"id":96896100,"name":{"sign":"YrFHYg==","value":"Kayserispor or X"}},{"name":{"value":"X or Artvin Hopaspor","sign":"knPxTA=="},"id":96896101,"status":"Visible","price":{"americanOdds":825,"denominator":4,"numerator":33,"id":145532959,"odds":9.25}},{"price":{"id":145532963,"odds":1.04,"americanOdds":-2500,"numerator":1,"denominator":25},"status":"Visible","name":{"sign":"fwDTwg==","value":"Kayserispor or Artvin Hopaspor"},"id":96896102}],"comboPrevention":"NoFixtureCombo","properties":[],"isMain":false,"status":"Visible","templateCategory":{"id":0,"category":"Gridable"},"grouping":{"parameters":{"period":"RegularTime","marketType":"DoubleChance","happening":"Goal"},"detailed":[{"marketTabId":1,"name":"Double Chance","group":0,"displayType":"Regular","marketHelpPath":"Football\/Double Chance","index":17230}],"gridGroups":["e8vi2hdwz"]}},{"options":[{"price":{"numerator":87,"denominator":100,"americanOdds":-115,"odds":1.87,"id":155223689},"status":"Visible","name":{"sign":"5Rageg==","value":"Over 3,5"},"id":100328998},{"price":{"numerator":5,"denominator":6,"americanOdds":-120,"odds":1.83,"id":155223693},"status":"Visible","id":100328999,"name":{"value":"Under 3,5","sign":"KP8xUw=="}}],"parameters":[{"type":"Decimal","value":"3.5000","key":"DecimalValue"},{"key":"Happening","value":"Goal","type":"String"},{"type":"String","value":"Over\/Under","key":"MarketType"},{"key":"Period","type":"String","value":"RegularTime"}],"name":{"value":"Total Goals","sign":"Y9HR2w=="},"id":30811485,"minCombo":1,"spread":0.04,"grouping":{"parameters":{"period":"RegularTime","marketType":"OverUnder","attr":"3.5000","happening":"Goal","attrType":"Decimal"},"detailed":[{"name":"Over\/Under Total Goals","marketTabId":1,"subIndex":4,"displayType":"OverUnder","index":27330,"group":0},{"subIndex":4,"marketTabId":1,"group":0,"displayType":"OverUnder","index":35130},{"group":2,"displayType":"OverUnder","index":11730,"subIndex":4,"marketTabId":1,"name":"Over\/Under Total Goals"},{"index":19530,"displayType":"OverUnder","group":2,"marketTabId":1,"subIndex":4}],"gridGroups":["tabpo1c9n"]},"balanced":1,"status":"Visible","attr":"3,5","isMain":false,"templateCategory":{"category":"Gridable","id":0},"properties":[],"comboPrevention":"NoFixtureCombo"}]}]}

Just use in php and Recursive Array Iterator. This will give you all fields and you don't need to iterate with foreach loops.

<?php
$json = '{"success":1,"pager":{"page":1,"per_page":250,"total":1813},"results":[{"Id":202322021,"SportId":4,"SportName":"Football","RegionId":31,"RegionName":"Turkey","LeagueId":102729,"LeagueName":"Turkiye Kupasi","BetRadarId":29664196,"our_event_id":4157451,"IsPreMatch":true,"Date":"2021-10-27T17:45:00Z","updated_at":"1635354806","HomeTeam":"Kayserispor","HomeTeamId":230391,"AwayTeam":"Artvin Hopaspor","AwayTeamId":228914,"Markets":[],"optionMarkets":[{"properties":[],"comboPrevention":"NoFixtureCombo","templateCategory":{"category":"Gridable","id":0},"status":"Visible","isMain":true,"grouping":{"gridGroups":["ls8o6ll2v"],"detailed":[{"name":"Match Result","marketTabId":1,"marketHelpPath":"Football\/Match Result (Regular Time)","index":7930,"displayType":"Regular","group":0}],"parameters":{"marketType":"ThreeWay","period":"RegularTime","happening":"Goal"}},"id":29622716,"name":{"value":"Match Result","sign":"9Vlc5w=="},"minCombo":1,"options":[{"name":{"value":"Kayserispor","sign":"tyg4wg=="},"id":96896088,"sourceName":{"value":"1"},"status":"Visible","price":{"id":145532907,"odds":1.055,"americanOdds":-2000,"denominator":18,"numerator":1}},{"price":{"id":145532911,"odds":11,"americanOdds":1000,"numerator":10,"denominator":1},"status":"Visible","name":{"value":"X","sign":"JHhicw=="},"id":96896089},{"price":{"id":145532915,"odds":31,"americanOdds":3000,"numerator":30,"denominator":1},"status":"Visible","sourceName":{"value":"2"},"id":96896090,"name":{"value":"Artvin Hopaspor","sign":"2ruL4g=="}}],"parameters":[{"key":"Happening","value":"Goal","type":"String"},{"key":"MarketType","value":"3way","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}]},{"minCombo":1,"id":29622721,"name":{"value":"Double Chance","sign":"WRDxhg=="},"parameters":[{"key":"Happening","type":"String","value":"Goal"},{"key":"MarketType","value":"DoubleChance","type":"String"},{"type":"String","value":"RegularTime","key":"Period"}],"options":[{"status":"Suspended","price":{"odds":1,"id":145532955},"id":96896100,"name":{"sign":"YrFHYg==","value":"Kayserispor or X"}},{"name":{"value":"X or Artvin Hopaspor","sign":"knPxTA=="},"id":96896101,"status":"Visible","price":{"americanOdds":825,"denominator":4,"numerator":33,"id":145532959,"odds":9.25}},{"price":{"id":145532963,"odds":1.04,"americanOdds":-2500,"numerator":1,"denominator":25},"status":"Visible","name":{"sign":"fwDTwg==","value":"Kayserispor or Artvin Hopaspor"},"id":96896102}],"comboPrevention":"NoFixtureCombo","properties":[],"isMain":false,"status":"Visible","templateCategory":{"id":0,"category":"Gridable"},"grouping":{"parameters":{"period":"RegularTime","marketType":"DoubleChance","happening":"Goal"},"detailed":[{"marketTabId":1,"name":"Double Chance","group":0,"displayType":"Regular","marketHelpPath":"Football\/Double Chance","index":17230}],"gridGroups":["e8vi2hdwz"]}},{"options":[{"price":{"numerator":87,"denominator":100,"americanOdds":-115,"odds":1.87,"id":155223689},"status":"Visible","name":{"sign":"5Rageg==","value":"Over 3,5"},"id":100328998},{"price":{"numerator":5,"denominator":6,"americanOdds":-120,"odds":1.83,"id":155223693},"status":"Visible","id":100328999,"name":{"value":"Under 3,5","sign":"KP8xUw=="}}],"parameters":[{"type":"Decimal","value":"3.5000","key":"DecimalValue"},{"key":"Happening","value":"Goal","type":"String"},{"type":"String","value":"Over\/Under","key":"MarketType"},{"key":"Period","type":"String","value":"RegularTime"}],"name":{"value":"Total Goals","sign":"Y9HR2w=="},"id":30811485,"minCombo":1,"spread":0.04,"grouping":{"parameters":{"period":"RegularTime","marketType":"OverUnder","attr":"3.5000","happening":"Goal","attrType":"Decimal"},"detailed":[{"name":"Over\/Under Total Goals","marketTabId":1,"subIndex":4,"displayType":"OverUnder","index":27330,"group":0},{"subIndex":4,"marketTabId":1,"group":0,"displayType":"OverUnder","index":35130},{"group":2,"displayType":"OverUnder","index":11730,"subIndex":4,"marketTabId":1,"name":"Over\/Under Total Goals"},{"index":19530,"displayType":"OverUnder","group":2,"marketTabId":1,"subIndex":4}],"gridGroups":["tabpo1c9n"]},"balanced":1,"status":"Visible","attr":"3,5","isMain":false,"templateCategory":{"category":"Gridable","id":0},"properties":[],"comboPrevention":"NoFixtureCombo"}]}]}';
$json_data = json_decode($json,true);

$data = array($json_data);
$obj = new ArrayObject($data);

$it = new RecursiveIteratorIterator( new RecursiveArrayIterator($data));

foreach ($it as $key=>$val)
echo $key.":".$val."\n";
Bernhard Beatus
  • 1,191
  • 1
  • 5
  • 6
0

You can display result of option Markets recursivelly, but the result will be a mess. You need to group up this in way you`ll like the most

foreach ($json_data['results'] as $key => $value) {
    if ($value['SportName'] == "Football") {
        echo "HomeTeam " . $value['HomeTeam'] . "  ";
        echo "AwayTeam " . $value['AwayTeam'] . "</br>";
        echo "LeagueName " . $value['LeagueName'] . "   " . $value['Date'] . "</br>";

        showOptionMarketsData($value['optionMarkets']);
    }
}

function showOptionMarketsData(array $optionMarkets)
{
    foreach ($optionMarkets as $val) {
        if (is_array($val)) {
            showOptionMarketsData($val);
        } else {
            echo "val " . $val . '</br>';
        }
    }
}
Roman Krut
  • 176
  • 8
  • This is 95% the solution I want. Thanks. Is it possible to also output the variable name before $val in the function. Something like: echo $item $val. thanks. – Ayo Owoade Oct 28 '21 at 07:10
  • Did you mean the key? If so, yes, you can, just modify foreach ($optionMarkets as $key => $val) { if (is_array($val)) { showOptionMarketsData($val); } else { echo "key $key val $val " . ''; } } – Roman Krut Oct 28 '21 at 07:49
  • Are you sure you copy the whole piece of code? Because variable $key is created in foreach body foreach ($optionMarkets as $key => $val) . Check it please – Roman Krut Oct 28 '21 at 09:39
  • It worked fine, thanks. – Ayo Owoade Oct 28 '21 at 10:10
  • I want to filter the output using say where $key=‘value’. But $key=“value” is not unique enough. Is it possible to have a third parameter in addition to $key and $val. I will like to do something like if ($key==“x” && $key1==“y”){do something;}. Thank you. – Ayo Owoade Oct 28 '21 at 16:10
  • Yes, you need to create so called mapping of key > value to filter outside of function. It can be simple array – Roman Krut Oct 28 '21 at 19:22
  • I apppreciate your help. I'm very new to json and will appreciate if you can help with the code for the mapping of key>value. Thanks. – Ayo Owoade Oct 28 '21 at 22:10