[英]Deriving the city and state from the location Facebook permission in Laravel 4
I am trying to separate out the city and state from the location array object from Facebook in Laravel. 我试图从Laravel中的Facebook的位置数组对象中分离出城市和州。 Before when I did this, I simply tried something similar to what I am trying in my Oauth2 Controller: 在进行此操作之前,我只是尝试了类似于Oauth2控制器中尝试的操作:
$citystate = $user['location'];
$citystate2 = $citystate['name'];
$split = explode(", ", $citystate2);
$city = $split[0];
$state = $split[1];
In the provider file, Facebook.php, I have this: 在提供程序文件Facebook.php中,我具有以下内容:
'location' => $user->location
However, when I run this, I get the error: 但是,当我运行此命令时,出现错误:
Use of undefined constant name - assumed 'name'
So, my question is, how do I access the "name" portion of the location array object. 因此,我的问题是,如何访问位置数组对象的“名称”部分。 Thank you for your help! 谢谢您的帮助!
At first, you should json_decode
(if it's a json string) like 首先,您应该像这样json_decode
(如果它是json字符串)
$userArray = json_decode($user, true);
So, you can use it like an array 因此,您可以像使用数组一样使用它
$citystate = $userArray['location'];
Also you can check if the $user
is already an array or not using 您也可以检查$user
是否已经是数组或不使用
if(is_array($user) && array_key_exists('location', $user)) {
// ...
$citystate = $user['location'];
// ...
}
Then you'll get an array like this 然后你会得到一个像这样的数组
array (size=2)
'id' => string 'xxx' (length=15) // xxx = digits
'name' => string 'Sylhet' (length=6)
Update : $user['location']
is an object : 更新: $user['location']
是一个对象:
object(stdClass)#180 (2) { ["id"]=> string(15) "114952118516947" ["name"]=> string(25) "San Francisco, California" }
So, it should be : 因此,应为:
$citystate = $user['location'];
$citystate->name;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.