[英]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" }
In PHP I have an array called $userData
as per above, I want to be able to echo values such as first name 'first_name', using code like this 在PHP中,我有一个如上所述的名为
$userData
的数组,我希望能够使用如下代码来回显诸如名“ first_name”之类的值
echo $userInfo->first_name;
(this doesnt seem to work) (这似乎不起作用)
I DO NOT want to loop and fetch all keys in array using foreach, just get values 'first_name', 'last_name' ect. 我不想循环使用的foreach获取所有的键在阵列中,只得到价值“FIRST_NAME”,“姓氏”等。 from the array
从数组
请这样尝试
$first_name = $userInfo['first_name'] // $userInfo is array name
Actually we use arrays
in programming to prevent loop or something like this to find a value, so instead of this structure, 实际上,我们在编程中使用
arrays
来防止循环或类似的事情来找到一个值,所以代替了这种结构,
$a = 'text1';
$b = 0;
$c = true;
$d = 'text2';
we use arrays: 我们使用数组:
$array = array('a' = > 'text1', 'b' => 0, 'c' => true, 'd' => 'text2' );
and to access the value of some key, we call the key name, inside brackets after array's name: 为了访问某些键的值,我们在数组名称后的方括号内调用键名称:
echo $array['a'];
//will echo text1
//or
echo $array['b'];
//will echo true
This is associative array
... 这是
associative array
...
if you just pass values to an array without keys, php will use numeric keys instead... 如果您只是将值传递给没有键的数组,则php会改用数字键...
$array = array( 'text1', '0', true, 'text2' );
$array[0] = 'text1';
$array[1] = 0;
$array[2] = true;
$array[3] = 'text2';
You can change the array to object and then use -> like below 您可以将数组更改为对象,然后使用->如下所示
$userData= (object) $userData;
And then $userData->firstName ..etc 然后$ userData-> firstName ..etc
Please use type casting. 请使用类型转换。
$userinfo = (array)$userInfo;
Then you access your key first_name. 然后,您访问密钥first_name。
echo $userinfo['first_name'];
First of use a different method to dump data .. 首先使用不同的方法转储数据..
echo "<pre>";
print_r($your_array);
exit;
It just simply bodes for a better presentation to see what is an object and what is an array. 它只是预示着一个更好的演示文稿,以了解什么是对象和什么是数组。
You cannot access array members with the object notation. 您不能使用对象符号访问数组成员。 You can however cast your array as an object.
但是,您可以将数组转换为对象。
(object)$yourarray;
If you want to access array members, use : 如果要访问数组成员,请使用:
$yourarray['first_name'];
If you cast as an object: 如果投射为对象:
$obj = (object)$yourarray
; $obj = (object)$yourarray
;
Now access: 现在访问:
$obj->firstname;
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.