0

I am using an API and am using a few foreach loops to get to the stage that I am at right now. Please see code below with my comments and also the results that I am getting below that.

// get recent_games api data
$recent_games_data = $player->recent_games();

//start arrays for below
$matches = array();
$gameType = array();
$myData = array();

// using foreach loops to dig in to api data
foreach($recent_games_data['gameStatistics']['array'] as $key_match_data => $value_match_data) {
    $matches[] = $value_match_data['statistics'];
}
foreach($matches as $key_match) {
    $gameType[] = $key_match['array'];
}
foreach ($gameType as $keyz) {
    $myData[] = $keyz;
}

The $mydata array outputs this data below.

Array
(
[0] => Array
    (
        [0] => Array
            (
                [statType] => TRUE_DAMAGE_DEALT_TO_CHARACTER
                [dataVersion] => 0
                [value] => 3351
                [futureData] => 
            )

        [1] => Array
            (
                [statType] => ASSISTS
                [dataVersion] => 0
                [value] => 14
                [futureData] => 
            )

        [2] => Array
            (
                [statType] => NUM_DEATHS
                [dataVersion] => 0
                [value] => 3
                [futureData] => 
            )
    )
[1] => Array
    (
        [0] => Array
            (
                [statType] => TRUE_DAMAGE_DEALT_TO_CHARACTER
                [dataVersion] => 0
                [value] => 331
                [futureData] => 
            )

        [1] => Array
            (
                [statType] => ASSISTS
                [dataVersion] => 0
                [value] => 4
                [futureData] => 
            )

        [2] => Array
            (
                [statType] => NUM_DEATHS
                [dataVersion] => 0
                [value] => 7
                [futureData] => 
            )
    )

Of course there is much more data but this is basically what I have now. The first array [0] is each match and the second array are the statistics for that match. What I want is how do I get the statistics of each match without hardcoding the match array number, for example below.

  $myData[0][0]['statType']

Let me know if you need more info and thank you.

EDIT: sorry for to mention that as new statistics data gets added to the api, the index number changes. IE TRUE_DAMAGE_DEALT_TO_CHARACTER is [0] to begin with but then may change to [1] or [2] etc.

1
  • you should consider working with model classes in stead of building a huge associative array. It might be a little bit more work at first, but it will make coding and maintenance so much easier. This is why OOP was invented! Commented Jun 12, 2013 at 22:37

2 Answers 2

1

Consider implementing a class for your stats items after parsing through the data (independent of individual match information keys):

class Stat_Item {
    function __construct($id, $info) {
        $this->id = $id;

        if(!empty($info['damage'])
            $this->damage_dealt = $info['damage'];

        if(!empty($info['assists']))
            $this->assists = $info['assists'];

        if(!empty($info['deaths']))
            $this->deaths = $info['deaths'];
    }
}

$parsed_items = array();

foreach($mydata as $match_id => $match) {
    $info = array();

    foreach($match as $data_point) {
        switch($data_point['statType']) {
            case TRUE_DAMAGE_DEALT_TO_CHARACTER:
                $info['damage'] = $data_point['value'];

                break;

            case ASSISTS:
                $info['assists'] = $data_point['value'];

                break;

            case NUM_DEATHS:
                $info['deaths'] = $data_point['value'];

                break;
        }

        $parsed_items[] = new Stat_Item($match, $info);
    }
}

Other than looping through them all, I don't see any way for you to get a particular match without calling it by its index.

Sign up to request clarification or add additional context in comments.

3 Comments

I've added more info on this, which will make the above invalid - thanks though!
Updated my answer with another potential solution.
This looks good - I'm definitely going to consider this. Thanks for the help
0

You don't need several foreach loops - you can add to all three arrays in a single one. Also, it looks like $gameType and $myData end up containing the same data.

foreach($recent_games_data['gameStatistics']['array'] as $key_match_data => $value_match_data) {
    $matches[] = $value_match_data['statistics'];
    $gameType[] = $value_match_data['statistics']['array'];
    $myData[] = $value_match_data['statistics']['array'];
}

I don't really understand why you don't just put it into the same array so you can access it easily, though:

foreach($recent_games_data['gameStatistics']['array'] as $key_match_data => $value_match_data) {
    $matches[] = array('statistics' => $value_match_data['statistics'], 'data' => $value_match_data['statistics']['array']);
}

3 Comments

Ah I see, that does make a lot of sense - I guess I was getting a tad frustrated and starting doing bad code just to get it working for now and then intended to clean it later. I've added a comment above, will paste here for you to see. sorry for to mention that as new statistics data gets added to the api, the index number changes. IE TRUE_DAMAGE_DEALT_TO_CHARACTER is [0] to begin with but then may change to [1] or [2] etc.
In that case, you may want to rewrite the data you receive from the API, so that instead of having [0] => Array ( [statType] => ASSISTS ) etc, you use $someArray['assists'] = array('dataversion' => $someVal, 'value' => $someVal /* etc */);. That way, you can easily update $matches[5]['assists'] = array(/* new values */); or similarly.
This helped big time as I have just made a breakthrough, thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.