[英]Associative Array get key value in PHP
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" }
在PHP中,我有一个如上所述的名为$userData
的数组,我希望能够使用如下代码来回显诸如名“ first_name”之类的值
echo $userInfo->first_name;
(这似乎不起作用)
我不想循环使用的foreach获取所有的键在阵列中,只得到价值“FIRST_NAME”,“姓氏”等。 从数组
请这样尝试
$first_name = $userInfo['first_name'] // $userInfo is array name
实际上,我们在编程中使用arrays
来防止循环或类似的事情来找到一个值,所以代替了这种结构,
$a = 'text1';
$b = 0;
$c = true;
$d = 'text2';
我们使用数组:
$array = array('a' = > 'text1', 'b' => 0, 'c' => true, 'd' => 'text2' );
为了访问某些键的值,我们在数组名称后的方括号内调用键名称:
echo $array['a'];
//will echo text1
//or
echo $array['b'];
//will echo true
这是associative array
...
如果您只是将值传递给没有键的数组,则php会改用数字键...
$array = array( 'text1', '0', true, 'text2' );
$array[0] = 'text1';
$array[1] = 0;
$array[2] = true;
$array[3] = 'text2';
您可以将数组更改为对象,然后使用->如下所示
$userData= (object) $userData;
然后$ userData-> firstName ..etc
请使用类型转换。
$userinfo = (array)$userInfo;
然后,您访问密钥first_name。
echo $userinfo['first_name'];
首先使用不同的方法转储数据..
echo "<pre>";
print_r($your_array);
exit;
它只是预示着一个更好的演示文稿,以了解什么是对象和什么是数组。
您不能使用对象符号访问数组成员。 但是,您可以将数组转换为对象。
(object)$yourarray;
如果要访问数组成员,请使用:
$yourarray['first_name'];
如果投射为对象:
$obj = (object)$yourarray
;
现在访问:
$obj->firstname;
希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.