CarlTaylor1989
CarlTaylor1989

Reputation: 209

Array confusion with square brackets

I am using an API which has a lot of data inside lots of arrays which as you may know can be quite confusing.I am relatively new to API's and this one in particular has no documentation.

My code below is grabbing the recent_games() function which is pulling the whole API then I am using foreach loops to get inside the data.

$games = $player->recent_games();

foreach($games['gameStatistics']['array'] as $key => $gameStatistic) {
    $game_date[strtotime($gameStatistic['createDate'])] = $gameStatistic;
}
// order data
krsort($game_date);

foreach ($game_date as $game => $data) {
    $statistics[$data] = $data['statistics'];
}

I am getting errors such as illegal offset for:

$statistics[$data] = $data['statistics'];

Is there a way to continue down the nesting of arrays ($game_date) to get to the data that I need?

Let me know if you need more info.

Thanks

EDIT more info:

The first foreach loop at the top loops a unix timestamp key per game. Looks like this:

[1370947566] => Array
    (
        [skinName] => Skin_name
        [ranked] => 1
        [statistics] => Array
            (
                 [array] => Array
                     (
                          [0] => Array
                              (
                                   [statType] => stat_data
                                   [value] => 1234
                              )
                          [1] => Array
                              (
                                   [statType] => stat_data
                                   [value] => 1234
                              )

As you can see its quite nested but I am trying to get to the individual statistics array. I hope that helps?

Upvotes: 0

Views: 112

Answers (1)

Jon
Jon

Reputation: 437386

$statistics[$data] = $data['statistics'];

There is absolutely no way this line is correct.

The right hand side uses $data as if it were an array, indexing into it. The left hand side uses $data as a key into an array. Since the only valid types for keys are strings and integers, $data cannot satisfy the requirements of both expressions at the same time -- it cannot be an array and a string or integer.

It's obvious from the error message that $data is in fact an array, so using it as $staticstics[$data] is wrong. What do you want $statistics to be?

Upvotes: 3

Related Questions