简体   繁体   English

如何访问Match数组中的所有Match

[英]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 . 我想记录所有在match数组中的匹配项。 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. 您可以使用对象引用( data.roundsround.matches )来访问对象的特定属性。 Then you can you array functions ( .forEach() which you can read about here ) to access the objects in each array. 然后,您可以在阵列功能( .forEach()你可以读到这里 )来访问每个阵列中的对象。 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. 根据Phil的评论,类似这样的事情应该可以帮助您遍历比赛并为每次比赛做一些事情。

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. 在该对象内部,有一个名为rounds的键,它也是对象的数组。

So data.rounds will give the value which is an array. 因此,data.rounds将给出一个数组值。

Inside this array there is array of matches. 在此数组内有匹配项数组。 But data.rounds is array of only one object. 但是data.rounds是只有一个对象的数组。 Hence data.rounds[0] will allow to access it's value. 因此, data.rounds[0]将允许访问其值。 [0] being the index, since in array the first element is at 0 index & data.rounds[0].matches will give the array of matches [0]是索引,因为数组中的第一个元素位于0 index&data.rounds [0] .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: 我认为您的数据是在JSON中构造的,您将使用json_decode函数将其解码,如下所示:

 $myMatches = json_decode($yourJSON);

After that JSON gets converted into an object you can access any property by following the following rules: 将JSON转换为对象后,您可以按照以下规则访问任何属性:

  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 JSON上的每个大括号都将转换为一个对象,您将必须使用如下箭头语法: $ myMatches- > attribute

For example, imagine an object "$person" with the following structure: 例如,假设对象“ $ person”具有以下结构:

 {
     "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'] 另一方面,每个普通括号都转换为数组,您必须使用括号来访问该信息(就像其他数组一样),如下所示:$ 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. 如果只有1个回合,则将执行此任务。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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