Using data in an array

删除回忆录丶 提交于 2020-01-15 11:56:07

问题


On my php page, I am getting the follow output:

Array ( [contact/email] => users_name@email_address.com )

This is produced via the following line in the php code:

print_r($openid->getAttributes());

How do I extract the text users_name@email_address.com from that array and stick it into a variable $strEmail;?

So when I echo the variable:

echo $strEmail;

It should output the following on screen:

users_name@email_address.com


回答1:


Assign the array to a variable and then you can easily access it:

$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];
echo $strEmail; // => users_name@email_address.com



回答2:


In your specific case, you can do:

$strEmail = reset($openid->getAttributes());

However it's better to suggest as this will work for other cases,too:

$attributes = $openid->getAttributes();
$strEmail = $attributes['contact/email'];

For more details see the PHP Manual about arrays how to access them.



来源:https://stackoverflow.com/questions/6556364/using-data-in-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!