dongshu4221 2010-12-07 21:54
浏览 167
已采纳

动态构建多维数组

I've been staring at this piece of code for nearly an hour and a half. In my inexperience I've no idea where I'm going wrong.

$gameInfo = array(
for($i=0; $i < 10; $i++){
    $friendsInfo[$i] = $facebook->api('/' . $randomfriends[$i]);
    array($randomfriends[$i],
          $friendsInfo[$i][first_name],
          $friendsInfo[$i][last_name],
          $friendsInfo[$i][hometown][name],
          $friendsInfo[$i][location][name],
          $friendsInfo[$i][about]
    );
    }
);

This is my attempt. As you can see the values are being pulled in from facebook. This is not the problem as each value appears if I echo. I want to take the values and put them into a multidimensional array that follows this structure.

$gameInfo =  array(
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about'),
    array('first_name','last_name','hometown','location','about')
);

Any help you can give me would be greatly appreciated

  • 写回答

3条回答 默认 最新

  • dtrvzd1171 2010-12-07 21:59
    关注

    Not quite sure what $facebook->api() gives you, but you probably want:

    $gameInfo = array();
    
    for($i=0; $i < 10; $i++){
        $gameInfo[] = $facebook->api('/' . $randomfriends[$i]);
    }
    

    or

    $gameInfo = array();
    
    for($i=0; $i < 10; $i++){
        $friend = $facebook->api('/' . $randomfriends[$i]);
        $gameInfo[] = array(
             $friend['first_name'],
             $friend['last_name'],
             $friend['hometown']['name'],
             $friend['location']['name'],
             $friend['about']
        );
    }
    

    Update: If api() returns a JSON string, then you have to use json_decode():

    $friend = json_decode($facebook->api('/' . $randomfriends[$i]), true);
    

    No offense, but your whole code is wrong syntax. You might want to read about arrays in PHP.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败