开发者

how to access an object (simpleXML) variable name using a string?

i need to access a simplexml object using a string. ie.

$x->a->b = 'obj'; $s = 'a->开发者_开发知识库;b'; echo $x->$s;

but it doesn't seem to work...

please help!

:)


You can do that like this, if my memory serves me:

echo $x->{$s};


you could use references:

$s =& $x->a->b;

or, if you want the string approach, build up the reference step by step:

function getRef($base, $str) {
    $out = $base;
    $parts = explode("->", $str);

    foreach ($parts as $p) {
        $out = $out->$p;
    }

    return $out;
}

getRef($x, "a->b");


That will not work. Are you trying to use xpath?

http://www.php.net/manual/en/simplexmlelement.xpath.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜