No array data from 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
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

No array data from function

Post by Todlerone »

Hello everyone and thank-you in advance for any help/suggestions. I have been trying to improve some of my baseball web site code. I seem to be having problems with populating my multdimensional array in my function. Originally I did a while loop with mysql fetch. What I tried to do was first read all of the DB data into an array. Then I used a foreach loop with a function call inside to parse the data. Here is some of thecode:

Code: Select all

foreach($stats as $stat): 
$standings=games($stat[0], $stat[1], $stat[2], $stat[3], $stat[4], $stat[5], $stat[6], $stat[7]);
endforeach;[
If I echo before the function call Isee the data I want.

Code: Select all

function games($vis, $home, $vis1, $home1, $vis2, $home2, $status1, $status2)
{
    global $outcome, $overall, $standings, $team, $num_teams;
        ...
        ...
        return ($standings);    
}
The function just doesn't do what it should. Can a function return a multidimensional array? Is the foreach function call the proper way to take my data array and produce my dynamic standings list?

TY
Cheers
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: No array data from function

Post by requinix »

Everything's fine except for

Code: Select all

$standings=games($stat[0], $stat[1], $stat[2], $stat[3], $stat[4], $stat[5], $stat[6], $stat[7]);
You keep reusing the same variable. That means on the second time through the loop it forgets what $standings was and gets the result for the second set of data.
Then immediately forgets it and gets the third set.
Then forgets that and gets the fourth set... All the way until $standings is the result from using the last set of data.

So:

Code: Select all

$standings=array(); // start with a blank array
foreach($stats as $stat):
// and add to it each time
$standings[]=games($stat[0], $stat[1], $stat[2], $stat[3], $stat[4], $stat[5], $stat[6], $stat[7]);
endforeach;
Post Reply