-2

This is what I have:

$namex = $xml->xpath("//a[@b=foo]/c");
$name = $namex[0];

echo $name;

It works, as the first line creates an array and the second line reads the first entry. Is there a way to combine the two lines to get the intended result right away?

Rubens
  • 13,978
  • 10
  • 59
  • 92
user1952748
  • 29
  • 1
  • 4
  • possible duplicate of [php simplexmlelement get first element in one line](http://stackoverflow.com/questions/2815639/php-simplexmlelement-get-first-element-in-one-line) – hakre Jul 13 '13 at 09:26
  • Recommended reading: [PHP syntax for dereferencing function result](http://stackoverflow.com/q/742764/367456) – hakre Jul 13 '13 at 10:02

2 Answers2

1

You can use list:

list($name) = $xml->xpath("//a[@b=foo]/c");
linepogl
  • 8,949
  • 4
  • 30
  • 44
0

Is there a way to combine the two lines to get the intended result right away?

Yes and no...

PHP 5.4+:

echo $xml->xpath("//a[@b=foo]/c")[0]; // array dereferencing, yay!

PHP < 5.4:

$names = $xml->xpath("//a[@b=foo]/c");
echo $names[0];
Jason McCreary
  • 69,176
  • 21
  • 125
  • 169