如何在 foreach 中使用 array_keys

发布于 2024-09-25 18:19:23 字数 605 浏览 6 评论 0原文

我需要从 array_keys 获取数据 我在服务器端使用的脚本:

PHP:

$friends = json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
   $facebook->getAccessToken() ), true);
$friend_ids = array_keys($friends);

数组的数据如上所示:

{
   "data": [
      {
         "name": "Tal Rozner",
         "id": "554089741"
      },
      {
         "name": "Daniel Kagan",
         "id": "559274789"
      },
  {
         "name": "ron cohen",
         "id": "100001553261234"
      }
   ]
}

我需要将所有这些数据获取到一个我可以使用它的数组。

我该怎么做? 坦克,

i need to get data from array_keys
the script i use in the server side:

PHP:

$friends = json_decode(file_get_contents(
'https://graph.facebook.com/me/friends?access_token=' .
   $facebook->getAccessToken() ), true);
$friend_ids = array_keys($friends);

the data of array look as above:

{
   "data": [
      {
         "name": "Tal Rozner",
         "id": "554089741"
      },
      {
         "name": "Daniel Kagan",
         "id": "559274789"
      },
  {
         "name": "ron cohen",
         "id": "100001553261234"
      }
   ]
}

i need to get all this data to an array that i can work with it.

how can i do it ?
tanks,

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

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

发布评论

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

评论(2

甚是思念 2024-10-02 18:19:23

如果我正确理解你的问题(我不确定我是否理解),你可能想要类似的东西,

$by_id = array();
foreach ($friends['data'] as $item) {
    $by_id[ $item['id'] ] = $item['name'];
}

它会给你和看起来像这样的数组:

print_r ($by_id);

Array
(
    [554089741] => Tal Rozner
    [559274789] => Daniel Kagan
    [100001553261234] => ron cohen
)

这可能更容易让你使用......

If i understand your question correctly (and I am not sure that I do) you might want something like

$by_id = array();
foreach ($friends['data'] as $item) {
    $by_id[ $item['id'] ] = $item['name'];
}

Which will give you and array that looks like this:

print_r ($by_id);

Array
(
    [554089741] => Tal Rozner
    [559274789] => Daniel Kagan
    [100001553261234] => ron cohen
)

Which might be easier for you to work with...

凉薄对峙 2024-10-02 18:19:23

不确定你的意思是“使用它”。如果来自 Facebook 的 JSON 响应是您发布的内容,您应该能够执行以下操作:

foreach ($friends['data'] as $friend) {
    echo "ID: {$friend['id']}" . PHP_EOL;
    echo "ID: {$friend['name']}" . PHP_EOL;
    echo PHP_EOL;
}

这将产生:

ID: 554089741
Name: Tal Rozner

ID: 559274789
Name: Daniel Kagan

ID: 100001553261234
Name: ron cohen

由于您使用了 json_decode(),$friends var 已经是一个数组了。在这种情况下,不需要 array_keys(),它只会生成 array (0, 1, 2)

Not sure what you mean "work with it." If the JSON response from Facebook is what you posted, you should be able to do this:

foreach ($friends['data'] as $friend) {
    echo "ID: {$friend['id']}" . PHP_EOL;
    echo "ID: {$friend['name']}" . PHP_EOL;
    echo PHP_EOL;
}

This would produce:

ID: 554089741
Name: Tal Rozner

ID: 559274789
Name: Daniel Kagan

ID: 100001553261234
Name: ron cohen

The $friends var would already be an array due to your use of json_decode(). In this case array_keys() isn't needed, and would only produce array (0, 1, 2).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文