problem returning array in a function

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
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

problem returning array in a function

Post by scheinarts »

Hey guys,

I have a function the gets some data from a DOM object but I cant seem the put it all in array to return that array object.

Code: Select all

    function runQuery()
    {
        
        foreach ($venues as $venue)
        {
            $names = $venue->getElementsByTagName('name');
            $contacts = $venue->getElementsByTagName('contact_link');
            
            foreach ($contacts as $contact)
            {
                $contact = getContact($contact->getAttribute('href'));
                $venueItem['contact_info'] = $contact;
            }
            
            foreach ($names as $name)
            {
                $venueItem['name'] = $name->nodeValue;
                
                if($name->getAttribute('href') != NULL)
                {
                    $address = getAddress($name->getAttribute('href'));
                }
                else
                {
                    $address = getAddress($contact['link']);
                }
                $venueItem['address_info'] = $address;
            }
            
            $venueData['item'] = $venueItem;
        }
    
        return $venueData;
    }
I excluded some variables that go before the for loop since they are not relevant to my question. If I print $venueData inside the for loop it works perfect. But if I do the following then I get nothing when printing it.

Code: Select all

$query = runQuery();
print_r($query);
And I need to be able to loop through the $query object, but it seems to be empty. What can I do here ?
Many thanks!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: problem returning array in a function

Post by pickle »

Print out $venueData at various points in your function to see that it's being built properly.

Also, please wrap your PHP code in [syntax=php][/syntax] tags, not the default [syntax=php][/syntax]
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: problem returning array in a function

Post by scheinarts »

Never figured out what the problem, so I just winded up recoding the function and other functions in the app. Thanks though!

I dont see the difference in the code tags???
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: problem returning array in a function

Post by pickle »

There - I've disabled BBCode parsing on my previous post. Sorry about that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: problem returning array in a function

Post by John Cartwright »

hint, $venues was never defined/passed to the function
Post Reply