我生成了这个数组:
代码:
$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 ) ) )
答案 0 :(得分:2)
您的输出以多维数组形式给出。
你可以尝试这样访问它:
// 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 :(得分:0)
foreach($yourArray as $array) {
echo $array['user_info']['first_name'];
}
答案 2 :(得分:0)
你需要这个吗?
echo $user_info[0]['first_name'];
答案 3 :(得分:0)
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']
它为您提供如下输出,具有特定的数组值。
如果要获取位于[1]上的位置的数组,则需要使用foreach
。