English 中文(简体)
如何在座标上使用
原标题:how to use in foreach for array_keys

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

阵列数据如下:

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

i 有必要将所有数据输入能够与之合作的阵列。

how can i do it ? tanks,

最佳回答

不知道你指的是“与它合作” 如果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() ,这已经是一个阵列。 在这种情况下,array_keys(> isn t need, and will only production array (0, 1, 2).

问题回答

如果我正确理解你的问题(我不相信我会这样做),你可能想要像你这样的东西。

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

可能更容易与......合作





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签