简体   繁体   English

从Laravel 4中的Facebook许可位置得出城市和州

[英]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.

相关问题 世博会地点中的城市位置 react-native - City location in expo location react-native 创建新城市 state object - create new city state object 美国城市/州阵列的简便方法 - easy way for USA city/state array 如何从数组 eloquent laravel 中获取权限名称值 - How to get permission name value from array eloquent laravel javascript正则表达式可获取始终跟随城市和逗号的2个字符的状态 - javascript regular expression to grab the state of 2 characters that is always following a city and a comma 在Laravel中使用Sentry动态添加权限 - Adding permission dynamically with Sentry in Laravel 地区,国家/地区,城市从Javascript中的CSV排序 - Region, Country, City sort from CSV in Javascript 从带有节点的字符串中提取城市名称 - extract city name from a string with node 如果用户 selected_city 也将来自 CITY MODEL 中未选择的城市,如何从用户已经选择的城市键的数组中获取结果 - How to get result from array in which user already selected_city key if user selected_city those will come also come not selected city from CITY MODEL 使用jq从其他数组派生一个数组(请参阅Python列表推导) - Deriving an array from other arrays with jq (cf. Python list comprehensions)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM