Page 1 of 1

Return first index of an array that has been returned by ...

Posted: Thu May 22, 2008 8:38 pm
by JellyFish
How do I return the first index of an array that was returned by a object method?

Code: Select all

 
$dom->get_elements_by_tagname("location")[0]
 
This gives me an error. O.o

How do I get the first element of the array that's returned by get_elements_by_tagname() method in one expression?

I know I could easily do this:

Code: Select all

 
$location = $dom->get_elements_by_tagname("location");
$location = $location[0];
 
But like I said I want it all in one expression.

Thanks for reading. :D

Re: Return first index of an array that has been returned by ...

Posted: Thu May 22, 2008 8:55 pm
by JellyFish
Solved it, never mind. It was easier then I thought.

Code: Select all

 
list($location) = $dom->get_elements_by_tagname("location");
 
If you have any better suggestions you can still post them.

Re: Return first index of an array that has been returned by ...

Posted: Thu May 22, 2008 9:47 pm
by Ambush Commander
List is a fairly compact way of doing it.

If it's really DOM you're working with, ->item(0) should also work.