json在动态父级中获取子级的值

json在动态父级中获取子级的值

问题描述:

Using PHP. How would I get clanAbbrev from a dynamic parent where it also contains "team" : "2"? I don't need to get it from every player, I just need to search it until I find the first player on Team 2. All players on team 2 are in the same "clan" and that is all I am trying to do is to get the clan tag of team 2.

"players":{  
        "524050468":{  
            "name":"Disqualification",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "511471621":{  
            "name":"RedBaron_GR",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "519208358":{  
            "name":"pokefast",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "501168520":{  
            "name":"mad_2fast4u",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "517090729":{  
            "name":"Tcoglani",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "505935551":{  
            "name":"Ultimate_Spinach_",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "505118732":{  
            "name":"VADOR2",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "504449266":{  
            "name":"MirageIVS",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "507177678":{  
            "name":"wolrdofAlexis",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "501356239":{  
            "name":"drcop",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "516847314":{  
            "name":"MaXiMiLiAn_Gr",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "517675972":{  
            "name":"hell_fighter_gr",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "512160116":{  
            "name":"XmorrayX",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "507147831":{  
            "name":"xxx_Anti_xxx",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "500190361":{  
            "name":"laskas",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "503220794":{  
            "name":"Sniker71240",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "503172799":{  
            "name":"PuNniShZz",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "504438396":{  
            "name":"Oulamagos1980",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"GRBB",
            "team":2,
            "clanDBID":500063884,
            "platoonID":19752025
        },
        "505065980":{  
            "name":"Fermierdu67",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        },
        "501645375":{  
            "name":"marcus68510",
            "prebattleID":19752025,
            "igrType":0,
            "clanAbbrev":"2DBFL",
            "team":1,
            "clanDBID":500029422,
            "platoonID":19752025
        }
    }

First of all, you need to make sure that you are converting your JSON to associative array using json_decode, as such:

json_decode($players, true)

Otherwise the keys will be data members and you will suffer. Then, implement a function like this:

function getClanAbbrevByTeam($players, $team) {
    foreach ($players as $player) {
        if ($player["team"] == $team) {
            return $player["clanAbbrev"];
        }
    }
    return false; //not found
}

The function searches for the first occurrence, assuming that a team might have a single clan abbreviation. You can call the function like this:

$clanAbbrev = getClanAbbrevByTeam(json_decode($players, true));