繁体   English   中英

如何使用多维数组?

[英]How to use a multi-dimensional array?

我生成了这个数组:

码:

$user_info = file_get_contents('http://localhost/rest-server/webservice/api.php?action=get_user&id='.$_GET["id"]);      
$user_info = json_decode($user_info, true);     
print_r($user_info); 

输出:

Array ( [0] => 
    Array ( [user_info] => 
        Array ( [id] => 12 
                [first_name] => Test1 
                [last_name] => User1 
                [email] => user1@example.com 
                [country] => ABC 
                [city] => XYZ 
              )
          )
      ) 

我该如何使用这个数组? 这样我就可以获取这样的数据:`$ user_info ['first_name'];

我想在这样的多个记录的数组上放置一个While循环:

Array ( [0] => Array ( [user_list] => Array ( [id] => 12 [first_name] => Test1 [last_name] => User1 [email] => user1@example.com [country] => ABC [city] => XYZ ) ) [1] => Array ( [user_list] => Array ( [id] => 13 [first_name] => Test2 [last_name] => User2 [email] => user2@example.com [country] => XYZ [city] => ABC ) ) ) 

您的输出以多维数组的形式给出。

你可以尝试这样访问它:

// Update to go through all items in $user_info:
foreach ($user_info as $index => $value)
{
    echo "For item {$index}:\n",
         "First name is: {$value['user_info']['first_name']}\n",
         "Email is     : {$value['user_info']['email']}\n";
    // etc.
}


// Old post to print individual record:

// Print the first name:
echo $user_info[0]['user_info']['first_name'];

// Print the email:
echo $user_info[0]['user_info']['email'];

// etc.

链接供您参考:

  1. PHP.net上的数组
  2. PHP.net on foreach()
foreach($yourArray as $array) {
    echo $array['user_info']['first_name'];
}

你需要这个 ?

echo  $user_info[0]['first_name'];
 echo $user_info[0]['user_info']['id'] 
 echo $user_info[0]['user_info']['first_name']
 echo $user_info[0]['user_info']['last_name'] 
 echo $user_info[0]['user_info']['email']
 echo $user_info[0]['user_info']['country'] 
 echo $user_info[0]['user_info']['city']

它为您提供如下特定数组值的输出。

  • 12
  • 测试1
  • 用户1
  • user1@example.com
  • ABC
  • XYZ

如果你想获取位于[1]上的数组,那么你必须使用foreach

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM