简体   繁体   中英

How to access all the matches inside matches array

{
  "name": "English Premier League 2015/16",
  "rounds": [
    {
      "name": "Play-Off um 1 Premierleague-Platz:",
      "matches": [
        {
          "date": "2015-08-08",
          "team1": {
            "key": "manutd",
            "name": "Manchester United",
            "code": "MUN"
          },
          "team2": {
            "key": "tottenham",
            "name": "Tottenham Hotspur",
            "code": "TOT"
          },
          "score1": 1,
          "score2": 0
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "bournemouth",
            "name": "Bournemouth",
            "code": "BOU"
          },
          "team2": {
            "key": "astonvilla",
            "name": "Aston Villa",
            "code": "AVL"
          },
          "score1": 0,
          "score2": 1
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "everton",
            "name": "Everton",
            "code": "EVE"
          },
          "team2": {
            "key": "watford",
            "name": "Watford",
            "code": "WAT"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "leicester",
            "name": "Leicester City",
            "code": "LEI"
          },
          "team2": {
            "key": "sunderland",
            "name": "Sunderland",
            "code": "SUN"
          },
          "score1": 4,
          "score2": 2
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "norwich",
            "name": "Norwich",
            "code": "NOR"
          },
          "team2": {
            "key": "crystalpalace",
            "name": "Crystal Palace",
            "code": "CRY"
          },
          "score1": 1,
          "score2": 3
        },
        {
          "date": "2015-08-08",
          "team1": {
            "key": "chelsea",
            "name": "Chelsea",
            "code": "CHE"
          },
          "team2": {
            "key": "swansea",
            "name": "Swansea",
            "code": "SWA"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "arsenal",
            "name": "Arsenal",
            "code": "ARS"
          },
          "team2": {
            "key": "westham",
            "name": "West Ham United",
            "code": "WHU"
          },
          "score1": 0,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "newcastle",
            "name": "Newcastle United",
            "code": "NEW"
          },
          "team2": {
            "key": "southampton",
            "name": "Southampton",
            "code": "SOU"
          },
          "score1": 2,
          "score2": 2
        },
        {
          "date": "2015-08-09",
          "team1": {
            "key": "stoke",
            "name": "Stoke City",
            "code": "STK"
          },
          "team2": {
            "key": "liverpool",
            "name": "Liverpool",
            "code": "LIV"
          },
          "score1": 0,
          "score2": 1
        },
        {
          "date": "2015-08-10",
          "team1": {
            "key": "westbrom",
            "name": "West Bromwich Albion",
            "code": "WBA"
          },
          "team2": {
            "key": "mancity",
            "name": "Manchester City",
            "code": "MCI"
          },
          "score1": 0,
          "score2": 3
        }
      ]
    }
  ]
}

I want to log all the matches which are inside matches array . But i can't seem to access them because there are objects ,arrays ,more arrays and more objects nested inside one another. Kinda confused.please help explaining how to access elements in such situation. Which loops to use, what to do in case of looping through objects and so on. Hope I have explained my problem quite elaborately.

You can try this this code here:

 const data = {"name":"English Premier League 2015/16","rounds":[{"name":"Play-Off um 1 Premierleague-Platz:","matches":[{"date":"2015-08-08","team1":{"key":"manutd","name":"Manchester United","code":"MUN"},"team2":{"key":"tottenham","name":"Tottenham Hotspur","code":"TOT"},"score1":1,"score2":0},{"date":"2015-08-08","team1":{"key":"bournemouth","name":"Bournemouth","code":"BOU"},"team2":{"key":"astonvilla","name":"Aston Villa","code":"AVL"},"score1":0,"score2":1},{"date":"2015-08-08","team1":{"key":"everton","name":"Everton","code":"EVE"},"team2":{"key":"watford","name":"Watford","code":"WAT"},"score1":2,"score2":2},{"date":"2015-08-08","team1":{"key":"leicester","name":"Leicester City","code":"LEI"},"team2":{"key":"sunderland","name":"Sunderland","code":"SUN"},"score1":4,"score2":2},{"date":"2015-08-08","team1":{"key":"norwich","name":"Norwich","code":"NOR"},"team2":{"key":"crystalpalace","name":"Crystal Palace","code":"CRY"},"score1":1,"score2":3},{"date":"2015-08-08","team1":{"key":"chelsea","name":"Chelsea","code":"CHE"},"team2":{"key":"swansea","name":"Swansea","code":"SWA"},"score1":2,"score2":2},{"date":"2015-08-09","team1":{"key":"arsenal","name":"Arsenal","code":"ARS"},"team2":{"key":"westham","name":"West Ham United","code":"WHU"},"score1":0,"score2":2},{"date":"2015-08-09","team1":{"key":"newcastle","name":"Newcastle United","code":"NEW"},"team2":{"key":"southampton","name":"Southampton","code":"SOU"},"score1":2,"score2":2},{"date":"2015-08-09","team1":{"key":"stoke","name":"Stoke City","code":"STK"},"team2":{"key":"liverpool","name":"Liverpool","code":"LIV"},"score1":0,"score2":1},{"date":"2015-08-10","team1":{"key":"westbrom","name":"West Bromwich Albion","code":"WBA"},"team2":{"key":"mancity","name":"Manchester City","code":"MCI"},"score1":0,"score2":3}]}]}; data.rounds.forEach((round) => { round.matches.forEach((match) => { console.log(`Results ${ match.score1 } | ${ match.score2 }`); }) }); 

Basically you're using a mixture of array and object references. You use your object references ( data.rounds or round.matches ) to get to specific properties on your object. Then you can you array functions ( .forEach() which you can read about here ) to access the objects in each array. Then you just access the properties of those sub objects.

Hope this helps.

Building on Phil's comment, something like this should help you iterate through the matches and do something with each.

obj.rounds[0].matches.forEach(match => {
    console.log(match);
})

The snippet shared is actually a object. Inside this object there is a key by name rounds , which is again array of objects.

So data.rounds will give the value which is an array.

Inside this array there is array of matches. But data.rounds is array of only one object. Hence data.rounds[0] will allow to access it's value. [0] being the index, since in array the first element is at 0 index & data.rounds[0].matches will give the array of matches

var data = {
  "name": "English Premier League 2015/16",
  "rounds": [{
    "name": "Play-Off um 1 Premierleague-Platz:",
    "matches": [
     //other objects
  ]
}
console.log(data.rounds[0].matches)

DEMO

 result = { "name": "English Premier League 2015/16", "rounds": [ { "name": "Play-Off um 1 Premierleague-Platz:", "matches": [ { "date": "2015-08-08", "team1": { "key": "manutd", "name": "Manchester United", "code": "MUN" }, "team2": { "key": "tottenham", "name": "Tottenham Hotspur", "code": "TOT" }, "score1": 1, "score2": 0 }, { "date": "2015-08-08", "team1": { "key": "bournemouth", "name": "Bournemouth", "code": "BOU" }, "team2": { "key": "astonvilla", "name": "Aston Villa", "code": "AVL" }, "score1": 0, "score2": 1 }, { "date": "2015-08-08", "team1": { "key": "everton", "name": "Everton", "code": "EVE" }, "team2": { "key": "watford", "name": "Watford", "code": "WAT" }, "score1": 2, "score2": 2 }, { "date": "2015-08-08", "team1": { "key": "leicester", "name": "Leicester City", "code": "LEI" }, "team2": { "key": "sunderland", "name": "Sunderland", "code": "SUN" }, "score1": 4, "score2": 2 }, { "date": "2015-08-08", "team1": { "key": "norwich", "name": "Norwich", "code": "NOR" }, "team2": { "key": "crystalpalace", "name": "Crystal Palace", "code": "CRY" }, "score1": 1, "score2": 3 }, { "date": "2015-08-08", "team1": { "key": "chelsea", "name": "Chelsea", "code": "CHE" }, "team2": { "key": "swansea", "name": "Swansea", "code": "SWA" }, "score1": 2, "score2": 2 }, { "date": "2015-08-09", "team1": { "key": "arsenal", "name": "Arsenal", "code": "ARS" }, "team2": { "key": "westham", "name": "West Ham United", "code": "WHU" }, "score1": 0, "score2": 2 }, { "date": "2015-08-09", "team1": { "key": "newcastle", "name": "Newcastle United", "code": "NEW" }, "team2": { "key": "southampton", "name": "Southampton", "code": "SOU" }, "score1": 2, "score2": 2 }, { "date": "2015-08-09", "team1": { "key": "stoke", "name": "Stoke City", "code": "STK" }, "team2": { "key": "liverpool", "name": "Liverpool", "code": "LIV" }, "score1": 0, "score2": 1 }, { "date": "2015-08-10", "team1": { "key": "westbrom", "name": "West Bromwich Albion", "code": "WBA" }, "team2": { "key": "mancity", "name": "Manchester City", "code": "MCI" }, "score1": 0, "score2": 3 } ] } ] }; for ( let i=0, totalRounds = result.rounds.length; i < totalRounds; i++) { let round = result.rounds[i]; console.log( round.name ); for ( let j=0, totalMatches = round.matches.length; j < totalMatches; j++ ) { let match = round.matches[j]; console.log( match.date + ': ' + match.team1.name + " " + match.score1 + " - " + match.team2.name + " " + match.score2) } } 

I guest that your data is structured in a JSON that you are going to decoded using the json_decode function like this:

 $myMatches = json_decode($yourJSON);

After that JSON gets converted into an object you can access any property by following the following rules:

  1. Every opening curly bracket on the JSON gets converted into an object, and you will have to use the arrow syntax like this: $myMatches -> attribute

For example, imagine an object "$person" with the following structure:

 {
     "name": "manutd",
     "lastname": "Manchester United",
     "phone": "MUN"
 }

To print the name you will have to do:

 echo $person->name;
  1. On the other hand, every normal bracket gets transformed into an array and you have to use brackets to access that information (like any other array), like this: $myMatches['attribute']

For example that same person but now with array syntax:

 [
     "name": "manutd",
     "lastname": "Manchester United",
     "phone": "MUN"
 ]

To print the name you will have to do:

echo $person['name'];

Now, for your question in particular

That said, if you want to print the date of the first round of the first match , you will have to do:

echo $myMatches->rounds[0]->matches[0]->date;

I have prepared this functions for your disposal:

//This function returns all the matches in one tournament round 
function getMatchesOfRound($myMatches, $roundNumber)
{
    return $myMatches->rounds[$roundNumber]->matches;
}

//This function returns all the matches played by $teamKey 
function getAllMatchesOfTeam($myMatches, $teamKey){
    $matches = [];
    foreach($myMatches->rounds as $rounds)
        foreach($rounds->matches as $match)
            if($match->team1->key == $teamKey || $match->team2->key == $teamKey) $matches[] = $match;

    return $matches;
}

//this function determines the winner of a given match and returns the $teamKey

function getWinerFromMatch($match){
    if($match->score1 > $match->score2) return $match->team1;
    else if($match->score1 < $match->score2) return $match->team2;
    else return null;
}

//This function returns all matches won by $teamKey
function getAllMatchesOfTeam($myMatches, $teamKey){
    $matches = [];
    foreach($myMatches->rounds as $rounds)
        foreach($rounds->matches as $match)
            if(getWinerFromMatch($match) == $teamKey) $matches[] = $match;
    return $matches;
}

//This function returns all ties
function getAllMatchesOfTeam($myMatches, $teamKey){
    $matches = [];
    foreach($myMatches->rounds as $rounds)
        foreach($rounds->matches as $match)
            if(getWinerFromMatch($match) == null) $matches[] = $match;

    return $matches;
}

You get the idea, I hope this helps. I have not tested the code, maybe it has minor sytax errors.

You can fetch all the matches in a variable then iterate over it using any loop. If there is only 1 round then this will do the task.

var matches = object.rounds[0]. matches;
for(var match in matches){
    console.log(match);
}

If there more rounds then you to first iterate over rounds and inside that loop fetch all the matches in that particular round.

for(var round in object.rounds){
    var matches = round.matches;
    for(var prop in matches){
        console.log(prop);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM