我有以下数组,它是从我的对象生成的
Laravel\Socialite\Two\User Object
(
[token] => some token valuw
[id] => 12345567769667
[nickname] =>
[name] => user name
[email] => username@test.com
[user] => Array
(
[kind] => plus
[gender] => male
[emails] => Array
(
[0] => Array
(
[value] => username@test.com
[type] => account
)
)
)
)
如果我尝试访问id昵称,名称,电子邮件,如
echo $user->id;
显示输出。 如果我试图访问
echo $user->user->gender;
或
echo $user['user']['gender'];
然后它将错误抛出为undefined index user
我如何获取性别和电子邮件价值?
谢谢
答案 0 :(得分:2)
$user
是object
而user
是array
if (property_exists($user, user) && !empty($user->user[gender])) {
$user->user[gender]; // to access gender
$user->user[emails]; // to access email
}
以避免通知和警告
答案 1 :(得分:1)
试试这个:
echo $user->user['gender'];
要访问电子邮件值:
$emailVal = [];
foreach($user->user['emails'] as $key => $val) {
$emailVal[] = ['value' => $val['value'], 'type' => $val['type']];
}
print_r($emailVal);
或者,如果您只想访问电子邮件:
echo $user->user['emails'][0]['value'];