如何在特定键值对之后访问stdclass对象?
我有一个 stdclass 对象,如下所示:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
我迭代多个此类对象,其中一些对象
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
我想知道如果“值”对象位于以“Parent”作为其值的“文本”之后,我将如何访问“值”对象?
I have a stdclass object as shown below:
stdClass Object
(
[text] => Parent
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Laurence W. Lane Jr.
[url] => http://www.freebase.com/view/m/0c02911
)
)
)
I iterate over multiple such objects, some of which have
stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
I was wondering how I would access the "values" object if it comes after a "text" that has "Parent" as its value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
有多种方法可以将其转换为数组:
第一个解决方案:
$value = get_object_vars($object);
第二个解决方案:
第三个解决方案
获取转换后的数组的值
访问对象var而不转换对象的另一种方法,尝试
there are serveral ways to turn it to array:
First Solution:
$value = get_object_vars($object);
Second Solution:
Third Solution
to get value of converted array
The alternate way to access objects var without convert the object, try
根据 Somwang Souksavatd 的回答进行扩展(或者更确切地说最小化),我喜欢像这样访问对象值:
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
我遇到了同样的问题,仍然不太确定为什么,但我能够使用此解决方法让它工作:
我不得不说,有时以数组形式访问元素有效,有时则无效,(在 PHP7 上它对我有用,但在 PHP5 上.6 没有)。
$elements
可以是数组,但我选择用 json 字符串进行演示。我希望这能有所帮助!
I had the same issue, still not so sure why but I was able to get it working using this workaround:
I have to say that sometimes accessing the element as array works and some times not,(On PHP7 it worked for me but on PHP5.6 it didn't).
$elements
can be Array to but I chose to demonstrate with json string.I hope this helps somehow !!!
我也在做同样的事情,我所做的就是这个;
I'm doing the same thing and all I did was this;
这可以帮助您使用 codeigniter 框架访问 php 中的子数组
this can help you accessing subarrays in php using codeigniter framework
您正在寻找的是 Object['values'][0]:'values' 是键盘映射,就像 'text' 一样,[0] 是您希望访问的数组内的索引。因此,如果您想获得巢穴深处的 id,您必须执行类似
or 的
操作,这应该会给您/m/0c02911。但我不知道你是如何进行循环的,因此你必须根据你的需要进行调整,并将适当的变量放置在循环中的代码中需要的位置。不太确定您正在使用哪种语言。
What you are looking for is the Object['values'][0]: 'values' is the keymap just like 'text', and [0] is the index inside that array you wish to access. so if you would like to get the id deep in the nest, you'd have to do something like
or
which should give you /m/0c02911. But I have no idea how you are doing your loop, so you will have to adjust it to your needs and place proper variables where they need to go in that code in your loop. Not exactly sure which language you are working with.