我通过以下方式从 simplexml-Object 中选择一个节点xpath
:
$x="<root>
<person>
<name>Julie</name>
<age>37</age>
<rating>5</rating>
</person>
<person>
<name>Greta</name>
<age>56</age>
<rating>7</rating>
</person>
<person>
<name>Kerstin</name>
<age>38</age>
<rating>10</rating>
</person>
</root>";
$xml = simplexml_load_string($x);
$person = $xml->xpath("//person[name='Greta']");
foreach ($person[0] as $k => $v)
echo "$k => $v<br />";
这是按预期输出...
name => Greta<br />age => 56<br />rating => 7<br />
那我就...
echo $person[0]['age'];
输出:
对,没什么!为什么?
现场体验@http: //3v4l.org/Voalu