2

I have a large array of strings that look like this:

$xml->archdesc->accessrestrict->p

These strings represent possible items in an XML document. The "$xml" is shorthand for the object(SimpleXMLElement) created when I read the XML file with simplexml_load_file().

How can I actually interpret these as variables? I want to access the value of each of these strings as they exist in the document, but I can't seem to make this happen. I've tried using the $$ format, the ${} format, eval(), and so on but nothing seems to give me the contents of the variable; they just display my string as a string.

Do these not work for things that use the object operator? Do I have to escape those in some way? Anything that I'm missing?

jlk
  • 21
  • 1
  • by the way, great question. although i wonder why anyone would store variable references in this way. seems like there should be a better way to accomplish whatever it is that you are trying to accomplish. – dqhendricks Jul 05 '11 at 23:40
  • I'm doing it this way because it allows me to loop through a list of XML elements and display the full variable name and if anything is currently assigned to that variable for debugging purposes. Easier to see than digging through the XML elements, allows me to sort them alphabetically, etc. – jlk Jul 05 '11 at 23:50
  • Did you know that you can loop through simplexml elements as if they were an array? Or even better, use an XPath query so you can loop through only the elements that match your XPath. – dqhendricks Jul 05 '11 at 23:53
  • How does one loop over all the simplexml elements in a document? – jlk Jul 06 '11 at 21:05

5 Answers5

2

Could you change your array to xpath locations instead?

$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><foo><bar>foobar</bar></foo><baz><boo>bazboo</boo></baz></root>");
$array = array('foo/bar', 'baz/boo');

foreach ($array as $xpath) { 
    $xpathResult = $xml->xpath($xpath);
    echo $xpathResult[0]."\n";
}

Although I definitely prefer the solution above, you could keep the existing array and use eval() (and read When is eval evil in php?)

$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><root><foo><bar>foobar</bar></foo><baz><boo>bazboo</boo></baz></root>");
$array = array('$xml->foo->bar', '$xml->baz->boo');
foreach ($array as $xpath) { 
    echo eval("return $xpath;");
}
Community
  • 1
  • 1
andyb
  • 43,435
  • 12
  • 121
  • 150
0

eval works for me.

Test Code:

$blah = new stdClass();
$blah->hello = 'hello';
$foo = '$blah->hello';
eval('echo '.$foo.';');
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

You can also use variable variables to accomplish this like so:

$blah = new stdClass();
$blah->hello = 'hello';
$foo = 'blah->hello';
$bar = explode('->', $foo);
echo $$bar[0]->$bar[1];

Notice you must trim the dollar sign off of the string before you can use it this way.

dqhendricks
  • 19,030
  • 11
  • 50
  • 83
0

No need for eval as suggested by other answers. Just do the following:

<?
$x = 'hello';
$y = new StdClass;
$z = 'y';
$y->hello = 'wes';
print $y->$x;
print $z->$x;
?>
Wes
  • 6,455
  • 3
  • 22
  • 26
0

Solved - my own dumb error; I was resetting the $xml var in a different spot and thus negating my use of the SimpleXML object, plus my eval() syntax was a bit off. Dumb error, completely unrelated to my question here. Sorry about that.

jlk
  • 21
  • 1