PHP中的关联数组获取键值

PHP中的关联数组获取键值,php,associative-array,Php,Associative Array,在PHP中,我有一个名为$userData的数组,如上所述,我希望能够使用如下代码来回显诸如first name‘first_name’之类的值 array(24) { ["user_id"]=> string(1) "9" ["facebook_id"]=> string(15) "381305418721463" ["first_name"]=> string(4) "John" ["last_name"]=> string(4) "Does" ["current_l

在PHP中,我有一个名为$userData的数组,如上所述,我希望能够使用如下代码来回显诸如first name‘first_name’之类的值

array(24) { ["user_id"]=> string(1) "9" ["facebook_id"]=> string(15) "381305418721463" ["first_name"]=> string(4) "John" ["last_name"]=> string(4) "Does" ["current_latitude"]=> string(10) "-37.825697" ["current_longitude"]=> string(10) "144.999965" ["current_address"]=> string(45) "229 Swan Street, Richmond VIC 3121, Australia" ["date_of_birth"]=> string(10) "01/01/1990" ["city"]=> string(30) "Melbourne, Victoria, Australia" ["country"]=> string(0) "" ["email_address"]=> string(22) "bzingatester@gmail.com" ["profile_pic"]=> string(0) "" ["first_login"]=> string(2) "no" ["blocked_users_id"]=> string(0) "" ["my_friend"]=> string(52) "10152805813948795,10155307822515151,1389504958030240" ["search_radius"]=> string(2) "50" ["device_type"]=> string(3) "ios" ["device_id"]=> string(1) "1" ["device_token"]=> string(64) "6ddaf9d59418e99b1c9cb28c21d94647bfed9f78a80b410164c1f2798beee84a" ["hideFromActivityFeed"]=> string(2) "no" ["hideFromFriendsofFriends"]=> string(2) "no" ["hideNotification"]=> string(2) "no" ["created_date"]=> string(19) "2015-01-07 01:00:11" ["modified_date"]=> string(19) "2015-02-27 05:36:12" }
echo $userinfo['first_name'];
这似乎不起作用

我不想使用foreach循环和获取数组中的所有键,只需要获取值“first\u name”、“last\u name”等。从阵列中,请像这样尝试

echo $userInfo->first_name; 
实际上,我们在编程中使用数组来防止循环或类似的东西来查找值,所以我们不使用这种结构

$first_name =  $userInfo['first_name'] // $userInfo is array name
我们使用数组:

$a = 'text1';
$b = 0;
$c = true;
$d = 'text2';
为了访问某个键的值,我们将其称为键名,在数组名称后的括号内:

$array = array('a' = > 'text1', 'b' => 0, 'c' => true, 'd' => 'text2' );
这是关联数组。。。 如果您只是将值传递给一个没有键的数组,php将使用数字键代替

echo $array['a'];
//will echo text1
//or
echo $array['b'];
//will echo true

您可以将数组更改为object,然后使用->如下所示

$array = array( 'text1', '0', true, 'text2' );

$array[0] = 'text1';
$array[1] = 0;
$array[2] = true;
$array[3] = 'text2';
然后
$userData->firstName..etc

请使用类型转换

  $userData= (object) $userData;
然后您访问您的密钥名

$userinfo = (array)$userInfo;

首先,请使用不同的方法转储数据

如果要访问数组成员,请使用:

$yourarray['first_name'];
如果强制转换为对象:

$obj=对象$yourarray

现在进入:

$obj->firstname;

希望这能有所帮助。

谢谢您的快速回复:不过似乎不起作用。页面抛出错误。。。顺便说一句,它在Laravel上运行,php端代码是$user=DB::table'users'->where'user\u id','=',$i->first;$userInfo=array$user;$first\u name=$userInfo['first\u name']/$userInfo是数组名var\u dump$userInfo;Codelarvel错误哎呀,有个错误。如果我取出你发给我的代码,页面仍会加载。谢谢你的帮助。仅供参考,此语句运行正常,我可以获取所有值,但不能单独获取,这是我希望foreach$userInfo为$x=>$x_value{echo Key=.$x.,value=.$x_value;echo;}的值。谢谢,现在我有一个新错误,无法使用stdClass类型的对象作为数组,突出显示上面的行。我想你把代码搞糟了,你能给我看看吗??您不能使用对象$userData然后使用$userData['firstName'],因为它不再是数组了。您确定要将$userData=object$userData;在使用$userData->firstName之前?如果您可以向我显示整个区号,而不仅仅是数组内容,这样我就可以计算出错误代码用户更改$userData['first_name']到$userData->first_name,正如您之前所说的,您想在数组中使用->这意味着您必须将其转换为object和$userData=object$userData;为您这样做,但您仍然使用数组如果您想要数组,您将需要删除$userData=object$userData;谢谢。我终于明白了。问题是返回的某些对象是空的。返回stdClass对象。我的问题是如何首先找到一个对象是否为空?如果我从一个空对象中提取,它会抛出一个错误。请尝试使用isset查看该对象是否存在。之后,您可以使用属性_exists。。调查那个特殊的财产。。
$yourarray['first_name'];
$obj->firstname;