1

将我的 JSON 解码为数组并在其上使用print_r后,我看到以下输出:

Array
(
    [users] => Array
        (
            [0] => Array
                (
                    [id] => 197849982
                    [url] => https://somewhere.zendesk.com/api/v2/users/197849982.json
                    [name] => Some User
                    [created_at] => 2012-04-15T00:38:33Z
                    [updated_at] => 2012-06-08T07:53:03Z
                    [time_zone] => Sydney
                    [email] => email@email.com
                    [phone] => 
                    [photo] => 
                    [locale_id] => 1
                    [locale] => en-US
                    [organization_id] => 21520772
                    [role] => admin
                    [verified] => 1
                    [external_id] => 
                    [tags] => Array
                        (
                        )

                    [alias] => 
                    [active] => 1
                    [shared] => 
                    [shared_agent] => 

有更多的输出,但我已经截断了它。

我需要访问诸如“id”和“name”之类的值并将它们放入变量中,但我不知道如何在该数组中获取它们。我尝试了几种方法,每次都得到不同的错误。

如何访问此数组中的数据?

4

2 回答 2

0

你是这个意思吗?

$id = $data['users'][0]['id']

我会假设您将拥有多个用户,所以我会做类似的事情

$users = array();
foreach($data['users'] as $user){
  $users[$user['id']] = $user['name'];
}
于 2013-08-28T21:53:40.817 回答
0

假设您在该结果集中只有一个用户:

$user = $arr['users'][0];

$id = $user['id'];
$name = $user['name'];

如果您有更多并且想要遍历它们,那么:

$users = $arr['users'];

foreach($users as $user){
    $id = $user['id'];
    $name = $user['name'];

    // Do something with each user here
}
于 2013-08-28T21:53:51.933 回答