PHP函数不会保存变量

时间:2011-05-09 04:15:11

标签: php arrays function

我有一个看起来像这样的函数:

function findSchedule($team)
{
    switch($team)
    {
        case "Baltimore Orioles":

        $team_home[42] = "Tampa Bay Rays";
        $team_home[43] = "Boston Red Sox";
        $team_home[44] = "Boston Red Sox";
        $team_home[45] = "$team";
        $team_home[46] = "$team";
        $team_home[47] = "$team";
        $team_home[48] = "$team";

        $team_away[42] = "$team";
        $team_away[43] = "$team";
        $team_away[44] = "$team";
        $team_away[45] = "New York Yankees";
        $team_away[46] = "New York Yankees";
        $team_away[47] = "Washington Nationals";
        $team_away[48] = "Washington Nationals";

        $team_date[42] = "Sun, May 15";
        $team_date[43] = "Mon, May 16";
        $team_date[44] = "Tue, May 17";
        $team_date[45] = "Wed, May 18";
        $team_date[46] = "Thu, May 19";
        $team_date[47] = "Fri, May 20";
        $team_date[48] = "Sat, May 21";

        break;

        case "Boston Red Sox":

        $team_home[42] = "$team";
        $team_home[43] = "$team";
        $team_home[44] = "$team";
        $team_home[45] = "$team";
        $team_home[46] = "$team";
        $team_home[47] = "$team";
        $team_home[48] = "$team";

        $team_away[42] = "Baltimore Orioles";
        $team_away[43] = "Baltimore Orioles";
        $team_away[44] = "Detroit Tigers";
        $team_away[45] = "Detroit Tigers";
        $team_away[46] = "Chicago Cubs";
        $team_away[47] = "Chicago Cubs";
        $team_away[48] = "Chicago Cubs";

        $team_date[42] = "Mon, May 16";
        $team_date[43] = "Tue, May 17";
        $team_date[44] = "Wed, May 18";
        $team_date[45] = "Thu, May 19";
        $team_date[46] = "Fri, May 20";
        $team_date[47] = "Sat, May 21";
        $team_date[48] = "Sun, May 22";

        break;
  }

  for($i = 42;$i < 49;++$i)
  {
    return $team_home[$i];
    return $team_away[$i];
    return $team_date[$i];
  }

当我尝试使用$ team_date,$ team_away和$ team_home变量时,唯一可行的是$ team_home变量。

$game = filter_input(INPUT_GET, 'game', FILTER_SANITIZE_STRING);

$team_home[$game] = findSchedule($team);
$team_away[$game] = findSchedule($team);
$team_date[$game] = findSchedule($team);

有什么想法吗?

谢谢,

兰斯

2 个答案:

答案 0 :(得分:3)

它与您构建最终for循环的方式有关。您将每个值设置为return; return语句结束处理。您希望每个echo然后返回,或者永远不要明确说明return,但只是结束该功能。

要返回所有参数,您只需创建一个更大的数组并返回:

return array( $team_home, $team_away, $team_date);

答案 1 :(得分:1)

在返回方面,你写了一个数组:

return array($team_home, $team_away, $team_date);

在接收方,您可以使用list()

list($team_home, $team_away, $team_date) = findSchedule($team);