如何在特定键值对之后访问stdclass对象?

发布于 2024-11-04 21:07:22 字数 792 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔一刀 2024-11-11 21:07:22

有多种方法可以将其转换为数组:

第一个解决方案:

$value = get_object_vars($object);

第二个解决方案:

$value = (array) $object;

第三个解决方案

$value = json_decode(json_encode($object), true);

获取转换后的数组的值

echo $value['values']['0']['id'];

访问对象var而不转换对象的另一种方法,尝试

$object->values->{'0'}->id

there are serveral ways to turn it to array:

First Solution:

$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

$object->values->{'0'}->id
七度光 2024-11-11 21:07:22

根据 Somwang Souksavatd 的回答进行扩展(或者更确切地说最小化),我喜欢像这样访问对象值:

echo get_object_vars($object)['values']['0']['id'];

Expanding (or rather minimalizing) upon answer by Somwang Souksavatd, I like accessing Object values like this:

echo get_object_vars($object)['values']['0']['id'];
独自←快乐 2024-11-11 21:07:22

我遇到了同样的问题,仍然不太确定为什么,但我能够使用此解决方法让它工作:

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

我不得不说,有时以数组形式访问元素有效,有时则无效,(在 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:

$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).

$elements can be Array to but I chose to demonstrate with json string.

I hope this helps somehow !!!

深居我梦 2024-11-11 21:07:22


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




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


明明#如月 2024-11-11 21:07:22

我也在做同样的事情,我所做的就是这个;

<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;

I'm doing the same thing and all I did was this;

<?php
$stdObject = json_decode($stdClassObject);
print $stdObject->values[0]->id;
不再让梦枯萎 2024-11-11 21:07:22

这可以帮助您使用 codeigniter 框架访问 php 中的子数组

foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
                                            echo $novo_puto_ultimos_30_dias;

this can help you accessing subarrays in php using codeigniter framework

foreach ($cassule['tarefa'][0] as $tarefa => $novo_puto_ultimos_30_dias) {
                                            echo $novo_puto_ultimos_30_dias;
国粹 2024-11-11 21:07:22

您正在寻找的是 Object['values'][0]:'values' 是键盘映射,就像 'text' 一样,[0] 是您希望访问的数组内的索引。因此,如果您想获得巢穴深处的 id,您必须执行类似

Object['values'][0]['id']

or 的

Object['values'][0]->id

操作,这应该会给您/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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文