动态构造多维数组

发布于 2024-10-07 04:07:47 字数 929 浏览 0 评论 0原文

我盯着这段代码看了将近一个半小时。由于我缺乏经验,我不知道我哪里出了问题。

$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]
    );
    }
);

这是我的尝试。正如您所看到的,这些值是从 Facebook 获取的。这不是问题,因为如果我回显,每个值都会出现。我想获取这些值并将它们放入遵循此结构的多维数组中。

$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')
);

您能给我的任何帮助将不胜感激

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

-残月青衣踏尘吟 2024-10-14 04:07:47

不太确定 $facebook->api() 为您提供了什么,但您可能想要:

$gameInfo = array();

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

$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']
    );
}

更新: 如果 api() 返回JSON字符串,那么你必须使用 json_decode()< /a>:

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

无意冒犯,但你的整个代码语法错误。您可能想阅读PHP 中的数组

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.

渔村楼浪 2024-10-14 04:07:47
$gameInfo = array();
for($i = 0; $i < 10; $i++){
    $friendsInfo[$i] = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
      $friendsInfo[$i][first_name],
      $friendsInfo[$i][last_name],
      $friendsInfo[$i][hometown][name],
      $friendsInfo[$i][location][name],
      $friendsInfo[$i][about]
    );
}

出于好奇,该代码是否应该像 python 列表理解一样?或者是来自另一种语言?

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

Out of curiosity was that code supposed to be like a python list comprehension? or was it from another language?

蓝礼 2024-10-14 04:07:47
$gameInfo = array();
for($i = 0; $i < 10; $i++){
    $friendsInfo = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
      $friendsInfo['first_name'],
      $friendsInfo['last_name'],
      $friendsInfo['hometown']['name'],
      $friendsInfo['location']['name'],
      $friendsInfo['about']
    );
}
$gameInfo = array();
for($i = 0; $i < 10; $i++){
    $friendsInfo = $facebook->api('/' . $randomfriends[$i]);
    $gameInfo[] = array(
      $friendsInfo['first_name'],
      $friendsInfo['last_name'],
      $friendsInfo['hometown']['name'],
      $friendsInfo['location']['name'],
      $friendsInfo['about']
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文