duanmu2015 2013-06-11 19:35
浏览 46
已采纳

数组混淆方括号

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?

  • 写回答

1条回答 默认 最新

  • dqm88684 2013-06-11 19:40
    关注
    $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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助