-2

i need to get specific value only from a key, example:

i need to get value of the "odd": "6.25" from

"name": "Team To Score Last"->"value": "No goal" 

and the ODD

this is my 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"
                        }

i tried with

$odds =json_decode($responseodds, true);
$value=$odds['response'][0]['bookmakers'][0]['bets'][0]['name'];

unfortunately i get only value Team To Score Last

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Fabio Krueger
  • 65
  • 1
  • 8
  • 1
    As I can read from the given JSON `['bets'][0]['name']` will not get _"Team To Score Last"_ because the first object of `bets` is empty. – Syscall Feb 04 '22 at 11:04
  • Please beautify your json with this: https://jsonformatter.curiousconcept.com/ It will be easier to see when something is array or object. – Robert Feb 04 '22 at 11:04
  • Is it relevant that you didnt post info in the `bets[0], bets[1], bets[2]......` arrays but you did in the `bets[11]` array? OR Do all those arrays look like `bets[11]` in reality? And therefore do you actually only want data from `bets[11]` or ALL these arrays – RiggsFolly Feb 04 '22 at 11:14
  • yes correct, i need to get bets[11], but the problem is that this value i need is not always on same position...but on next array could be on bets[8]. for this reason i ask to get value by name Team To Score Last->Team To Score Last->6.25 – Fabio Krueger Feb 04 '22 at 11:18
  • But you dont show us the `Name of Team` in any of these data items – RiggsFolly Feb 04 '22 at 11:20
  • Of do you mean you are looking for the Odds for the `Team to Score Last` – RiggsFolly Feb 04 '22 at 11:21
  • @RiggsFolly yes, looking odd for the Team to Score Last – Fabio Krueger Feb 04 '22 at 11:23

2 Answers2

2

Loop over the bets array, and look for the bet name you are interested in.

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

If you want these odds for all of the bookies

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'];
        }
    }
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
-1
$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);
}

I've try this and I've the Odd value

Kallard
  • 57
  • 7