简体   繁体   English

从PHP中的多维数组检索值

[英]Retrieve value from a multi-dimensional array in PHP

I have a multidim array that I can view using print_r($users) : 我有一个可以使用print_r($ users)查看的多维数组:

Array
(
    [0] => stdClass Object
        (
            [username] => crustacea
            [created_on] => 2010-08-07 19:54:32
            [active] => 1
        )
)

I can also use print_r($users[0]) . 我也可以使用print_r($ users [0]) Since there's only one member in $users, the output looks somewhat the same. 由于$ users中只有一个成员,因此输出看起来有些相同。

I can't see how to retrieve the value crustacea . 我看不到如何获取甲壳动物的价值。 echo $users[0]['username']; echo $ users [0] ['username']; doesn't do it. 不这样做。 Examples I can find are echo $users['name_of_array']['username']; 我可以找到的示例是echo $ users ['name_of_array'] ['username']; -- but I don't have a name of array to work with. -但我没有要使用的数组名称。 How do I do it? 我该怎么做?

$ user [0]不是数组,而是[0] => stdClass Object指示的[0] => stdClass Object

echo $users[0]->username;

$user[0] is object and not an array. $ user [0]是对象,而不是数组。 You can access your value by doing 您可以通过以下方式获取价值

$users[0]->username;

Or you could cast/convert your object into an array like 或者您可以将对象转换/转换为类似的数组

((array)$users[0])['username'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM