php 如何访问数组的对象(stdClass 对象)成员/元素的属性?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21168422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to access a property of an object (stdClass Object) member/element of an array?
提问by Alex
Doing print_r()on my array I get the following:
print_r()在我的阵列上做我得到以下信息:
Array (
[0] =>
stdClass Object
(
[id] => 25
[time] => 2014-01-16 16:35:17
[fname] => 4
[text] => 5
[url] => 6
)
)
How can I access a specific value in the array? The following code does not work because of the stdClass Object
如何访问数组中的特定值?由于 stdClass 对象,以下代码不起作用
echo $array['id'];
回答by Lucky Soni
To access an array member you use $array['KEY'];
要访问您使用的数组成员 $array['KEY'];
To access an object member you use $obj->KEY;
要访问您使用的对象成员 $obj->KEY;
To access an object member inside an array of objects:$array[0]// Get the first object in the array$array[0]->KEY// then access its key
访问对象数组中的对象成员:$array[0]// 获取数组中的第一个对象$array[0]->KEY// 然后访问其键
You may also loop over an array of objects like so:
您还可以循环遍历对象数组,如下所示:
foreach ($arrayOfObjs as $key => $object) {
echo $object->object_property;
}
Think of an array as a collection of things. It's a bag where you can store your stuff and give them a unique id (key) and access them (or take the stuff out of the bag) using that key. I want to keep things simple here, but this bag can contain other bags too :)
将数组视为事物的集合。这是一个袋子,您可以在其中存储您的东西并给它们一个唯一的 ID(密钥)并使用该密钥访问它们(或从袋子中取出东西)。我想在这里保持简单,但这个包也可以装其他包:)
Update (this might help someone understand better):
更新(这可能有助于某人更好地理解):
An array contains 'key' and 'value' pairs. Providing a key for an array member is optional and in this case it is automatically assigned a numeric key which starts with 0 and keeps on incrementing by 1 for each additional member. We can retrieve a 'value' from the array by it's 'key'.
一个数组包含“ key”和“ value”对。为数组成员提供键是可选的,在这种情况下,它会自动分配一个数字键,该键从 0 开始,并为每个附加成员继续增加 1。我们可以通过它的“ key”从数组中检索一个“值”。
So we can define an array in the following ways (with respect to keys):
所以我们可以通过以下方式定义一个数组(关于键):
First method:
第一种方法:
$colorPallete = ['red', 'blue', 'green'];
The above array will be assigned numeric keys automatically. So the key assigned to red will be 0, for blue 1 and so on.
上面的数组将自动分配数字键。因此分配给红色的键将为 0,蓝色为 1,依此类推。
Getting values from the above array:
从上述数组中获取值:
$colorPallete[0]; // will output 'red'
$colorPallete[1]; // will output 'blue'
$colorPallete[2]; // will output 'green'
Second method:
第二种方法:
$colorPallete = ['love' => 'red', 'trust' => 'blue', 'envy' => 'green']; // we expliicitely define the keys ourself.
Getting values from the above array:
从上述数组中获取值:
$colorPallete['love']; // will output 'red'
$colorPallete['trust']; // will output 'blue'
$colorPallete['envy']; // will output 'green'
回答by Rohit Suthar
Try this, working fine -
试试这个,工作正常 -
$array = json_decode(json_encode($array), true);
回答by giordanolima
Try this:
尝试这个:
echo $array[0]->id;
回答by Alan Storm
You have an array. A PHP array is basically a "list of things". Your array has one thing in it. That thing is a standard class. You need to either remove the thing from your array
你有一个数组。PHP 数组基本上是一个“事物列表”。您的阵列中只有一件事。那东西是标准班。您需要从阵列中删除该东西
$object = array_shift($array);
var_dump($object->id);
Or refer to the thing by its index in the array.
或者通过它在数组中的索引来引用事物。
var_dump( $array[0]->id );
Or, if you're not sure how many things are in the array, loop over the array
或者,如果您不确定数组中有多少内容,请遍历数组
foreach($array as $key=>$value)
{
var_dump($value->id);
var_dump($array[$key]->id);
}
回答by Nomi
How about something like this.
这样的事情怎么样。
function objectToArray( $object ){
if( !is_object( $object ) && !is_array( $object ) ){
return $object;
}
if( is_object( $object ) ){
$object = get_object_vars( $object );
}
return array_map( 'objectToArray', $object );
}
and call this function with your object
并用你的对象调用这个函数
$array = objectToArray( $yourObject );

