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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

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

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

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

Post 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.
Post Reply