【问题标题】:PHP function wont save variablesPHP函数不会保存变量
【发布时间】:2011-05-09 04:15:10
【问题描述】:

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

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);

有什么想法吗?

谢谢,

兰斯

【问题讨论】:

  • 与您的问题没有直接关系,但如果您使用 switch 语句来处理两个以上的团队,那么这听起来确实像是数据库会派上用场的工作......
  • 是的,实际上是 30 个团队。但是,我真的不想对数据库施加太大压力。但是,你认为在这种情况下依赖 DB 会比依赖 php 更有效吗?
  • 30 个团队不会对您的数据库造成压力。试试3000万左右:)
  • 太棒了。我可能会改用 mysql 多一点。附带说明一下,每页使用超过 3 个查询通常是不好的做法吗?这会大大减少加载时间吗?

标签: php arrays function


【解决方案1】:

这与您构建最终for 循环的方式有关。您将其设置为 return 每个值; return 语句结束处理。你想echo每一个然后返回,或者从不明确声明return而只是结束函数。

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

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

【讨论】:

  • 那么,如何保存数组中的值而不返回它们?
【解决方案2】:

在返回端你写一个数组:

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

而在接收端你可以使用list():

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-06
    • 1970-01-01
    • 1970-01-01
    • 2012-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多