【问题标题】:Accessing data in PHP object by numeric key通过数字键访问 PHP 对象中的数据
【发布时间】:2012-10-05 21:44:22
【问题描述】:

我在一个我不知道如何访问它的数据的对象中“突出显示”了一个 solr 查询的输出。假设我想找回我尝试过的“Black & Red”:

$object->{10304}->color_source_name[0];
$object->{'10304'}->color_source_name[0];

两者都没有给我任何东西。如何访问数据?

这是我在下面引用的对象:

print_r($object);

SolrObject Object
(
[10304] => SolrObject Object
    (
        [color_source_name] => Array
            (
                [0] => Black & Red
            )

        [description] => Array
            (
                [0] =>  with their true identity safely masked.Midcalf Lucha Red
            )

        [name] => Array
            (
                [0] => Sock It To Me Lucha Red Midcalf
            )

        [color] => Array
            (
                [0] => Red
            )

    )

[4075] => SolrObject Object
    (
        [color_source_name] => Array
            (
                [0] => Beige/Red
            )

        [description] => Array
            (
                [0] => Very sheer with red tops and Cuban heels and an oh-so-sexy red backseam.  These are designed
            )

        [name] => Array
            (
                [0] => Two-Tone Red Cuban-Heeled Stockings
            )

        [color] => Array
            (
                [0] => Red
            )

    )

)

【问题讨论】:

    标签: php solr


    【解决方案1】:

    根据http://php.net/manual/en/class.solrobject.php SolrObject 实现ArrayAccess 所以你可以使用方括号,例如

    echo $object[4075]['colour_source_name'][0];
    

    【讨论】:

    • 我知道这应该是可能的,但它从来没有奏效。我最终不得不做一个 foreach 循环并以这种方式拔出钥匙。没有意义,但这就是发生的事情。
    • 我不能说。我最终没有直接访问它。但就像我说的那样,我必须通过循环将其拉出。
    • @danjfoley 您能否提供您的解决方案并接受它,以便将来的访问者可以从中受益,以防我对手册的阅读有误。
    猜你喜欢
    • 2016-05-18
    • 2011-09-18
    • 2012-07-01
    • 2011-02-23
    • 2012-02-03
    • 1970-01-01
    • 2012-02-28
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多