php 如何在特定键值对后访问 stdclass 对象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5875785/
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 stdclass object after a specific key value pair?
提问by Rio
I have a stdclass object as shown below:
我有一个 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
)
)
)
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?
我想知道如果“值”对象出现在以“父级”作为其值的“文本”之后,我将如何访问它?
采纳答案by robx
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
您正在寻找的是 Object['values'][0]: 'values' 是键映射,就像 'text' 一样,而 [0] 是您希望访问的数组中的索引。因此,如果您想将 id 深入到巢中,则必须执行以下操作
Object['values'][0]['id']
or
或者
Object['values'][0]->id
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.
这应该给你/m/0c02911。但是我不知道您是如何进行循环的,因此您必须根据需要对其进行调整,并将适当的变量放置在循环中该代码中需要放入的位置。不确定您正在使用哪种语言。
回答by Somwang Souksavatd
there are serveral ways to turn it to array:
有几种方法可以将其转换为数组:
First Solution:
第一个解决方案:
$value = get_object_vars($object);
$value = get_object_vars($object);
Second Solution:
第二种解决方案:
$value = (array) $object;
Third Solution
第三种解决方案
$value = json_decode(json_encode($object), true);
to get value of converted array
获取转换数组的值
echo $value['values']['0']['id'];
The alternate way to access objects var without convert the object, try
在不转换对象的情况下访问对象 var 的替代方法,请尝试
$object->values->{'0'}->id
回答by user3298746.a.lot
Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:
根据 Somwang Souksavatd 的回答扩展(或者说是最小化),我喜欢像这样访问对象值:
echo get_object_vars($object)['values']['0']['id'];
回答by Pini Cheyni
I had the same issue, still not so sure why but I was able to get it working using this workaround:
我遇到了同样的问题,仍然不太确定为什么,但我能够使用以下解决方法使其正常工作:
$k2 ="1";
$elements = json_decode('{"id":"1","name":"User1"}');
//$elements['id'] == $k2; //****Not Working
$tmp = (object)$elements;
$tmp = $tmp ->id; //****Working
//$tmp =$elements['id'] ; //****Not Working
return $tmp == $k2;
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).
我不得不说,有时将元素作为数组访问,有时则不起作用(在 PHP7 上它对我有用,但在 PHP5.6 上却没有)。
$elements
can be Array to but I chose to demonstrate with json string.
$elements
可以是 Array to 但我选择用 json 字符串来演示。
I hope this helps somehow !!!
我希望这会有所帮助!!!
回答by Nguy?n V?n Th?ng
$Obj=stdClass Object
(
[text] => Named after
[values] => Array
(
[0] => stdClass Object
(
[id] => /m/0c02911
[text] => Stanford
[url] => SomeURL
)
)
)
$Values= $result->values;
$Item = $Values[0];
$id=$Item->id;
$text = $Item->text;
$url=$Item->url;
回答by pythonian29033
I'm doing the same thing and all I did was this;
我正在做同样的事情,我所做的就是这样;
<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;