如何访问对象(stdClass的对象)的成员/数组的元素的属性? [英] How to access a property of an object (stdClass Object) member/element of an array?

查看:459
本文介绍了如何访问对象(stdClass的对象)的成员/数组的元素的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的print_r()我的阵列上我得到如下:

 阵列(
    [0] =>
        stdClass的对象
        (
            [ID] => 25
            [时间] => 2014年1月16日十六时35分17秒
            [FNAME] => 4
            [文] =>五
            [URL] => 6
        )

我怎么可以在阵列中访问特定的价值?下面code没有因为stdClass的对象的工作

 回声$阵列['身份证'];


解决方案

要访问您使用数组成员 $数组['钥匙'];

要访问您使用对象成员 $ obj->关键;

要访问对象的数组内的对象的成员:结果
$数组[0] //获取数组结果的第一个对象
$数组[0] - > KEY //然后访问其关键

您还可以循环一个对象,像这样的数组:

 的foreach($ arrayOfObjs为$关键=> $对象){
    回声$对象 - > object_property;
}

将数组视为事物的集合。这是一个包,你可以使用该密钥存储你的东西,并给他们一个唯一的ID(键)和访问它们(或取东西的袋子)。我希望让事情变得简单在这里,但这个包可以包含其他袋太:)

更新(这可能会帮助别人更好地理解):

这是阵列包含'对。提供阵列构件的一个关键是可选的,并且在此情况下,它是自动分配其中以0开始,并保持对各1个附加构件递增的数字键。我们可以从该数组检索一个价值由它的

因此​​,我们可以定义在以下几个方面的阵列(相对于键):

第一种方法:

  $ colorPallete = ['红','蓝','绿色'];

以上阵列将被自动分配数字键。因此分配给红色的关键将是0,适用于蓝色1等

从上面的阵列获取的值:

  $ colorPallete [0]; //将输出红色
$ colorPallete [1]; //将输出蓝
$ colorPallete [2]; //将输出绿色

方法二:

  $ colorPallete = ['爱'=> '红','信任'=> '蓝','嫉妒'=> '绿色']; //我们expliicitely定义按键我们自己。

从上面的阵列获取的值:

  $ colorPallete ['爱']; //将输出红色
$ colorPallete ['信任']; //将输出蓝
$ colorPallete ['嫉妒']; //将输出绿色

Doing print_r() on my array I get the following:

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

echo $array['id'];

解决方案

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

To access an object member you use $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

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

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

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.

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'

这篇关于如何访问对象(stdClass的对象)的成员/数组的元素的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆