Page 1 of 1

No array data from function

Posted: Fri May 15, 2009 9:07 pm
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

Re: No array data from function

Posted: Fri May 15, 2009 10:08 pm
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;