My code
<?php
$a = 'node'
$xml = simplexml_load_file('config.xml');
echo $xml->node //work fine
echo $xml->{$a}; //also work fine
?>
<?php
$a = 'node[0]'
$xml = simplexml_load_file('config.xml');
echo $xml->node[0] //work fine
echo $xml->{$a}; //DOESNT WORK!
?>
Why node[0] in a variable not work ? how can I solve?
nodeis the actual property name of the property that you want to access. When you usednode[0]instead ofnode, it looked for the property callednode[0]instead of accessing the 0th index of the array$xml->node. Basically, It looked fornode[0]as if you typed$xml->{"node[0]"}instead ofnode(which is what you’re expecting to do), and it can’t find it. So you’d want to try something like this instead: