如何访问数组的对象(stdClass对象)成员/元素的属性?(复制)

时间:2022-10-23 08:03:41

This question already has an answer here:

这个问题已经有了答案:

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'];

5 个解决方案

#1


62  

To access an array member you use $array['KEY'];

要访问数组成员,需要使用$array['KEY'];

To access an object member you use $obj->KEY;

要访问对象成员,请使用$obj->键;

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 (key),然后用这个key访问它们(或者把东西从袋子里拿出来)。我想在这里保持简单,但是这个袋子也可以装其他的袋子。

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'.

数组包含“键”和“值”对。为数组成员提供一个键是可选的,在这种情况下,它会自动分配一个数字键,它从0开始,并对每一个额外的成员继续递增1。我们可以通过数组的'key'从数组中检索'value'。

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'

#2


14  

Try this, working fine -

试试这个,效果很好

$array = json_decode(json_encode($array), true);

#3


9  

Try this:

试试这个:

echo $array[0]->id;

#4


3  

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);
}

#5


0  

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 );

reference

参考

#1


62  

To access an array member you use $array['KEY'];

要访问数组成员,需要使用$array['KEY'];

To access an object member you use $obj->KEY;

要访问对象成员,请使用$obj->键;

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 (key),然后用这个key访问它们(或者把东西从袋子里拿出来)。我想在这里保持简单,但是这个袋子也可以装其他的袋子。

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'.

数组包含“键”和“值”对。为数组成员提供一个键是可选的,在这种情况下,它会自动分配一个数字键,它从0开始,并对每一个额外的成员继续递增1。我们可以通过数组的'key'从数组中检索'value'。

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'

#2


14  

Try this, working fine -

试试这个,效果很好

$array = json_decode(json_encode($array), true);

#3


9  

Try this:

试试这个:

echo $array[0]->id;

#4


3  

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);
}

#5


0  

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 );

reference

参考