PHP 数组在函数中不起作用

发布于 2024-10-09 06:51:08 字数 2220 浏览 3 评论 0原文

我目前正在 PHP 中尝试数组,并且创建了一个虚假的环境,将在其中显示团队的信息。

    $t1 = array (
            "basicInfo" => array (
                "The Sineps",
                "December 25, 2010",
                "lemonpole"
            ),
            "overallRecord" => array (0, 0, 0, 0),
            "overallSeasons" => array (
                1 => array (14, 0, 0),
                2 => array (9, 5, 2),
                3 => array (12, 4, 0),
                4 => array (3, 11, 2)
            ),
            "games" => array (
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />"
            ),
            "seasonHistory" => array (
                "Season I",
                "Season II",
                "Season III",
                "Season IV"
            ),
            "divisions" => array (
                "Open",
                "Main",
                "Main",
                "Invite"
            )
        );
// Displays the seasons the team has been in along
    // with the record of each season.
    function seasonHistory() {
        // Make array variable local-scope.
        global $t1;

        // Count the number of seasons.
        $numrows = count($t1["seasonHistory"]);

        // Loop through all the variables until
        // it reaches the last entry made and display
        // each item seperately.
        for($v = 0; $v <= $numrows; $v++) {
            // Echo each season.
            echo "<tr><td>{$t1["games"][$v]}</td>";
            echo "<td>{$t1["seasonHistory"][$v]}</td>";
            echo "<td>{$t1["divisions"][$v]}</td></tr>";
        }
    }

我测试了几个可能的问题,在缩小范围后,我得出了一个结论,那就是我的函数由于某种原因没有连接到数组。我不知道还能做什么,因为我认为使数组全局化可以解决这个问题。

有效方法:

  1. 我可以在需要显示的页面上回显$t1["games"][0],它会为我提供内容。

  2. 我尝试了 echo $t1["games"][0] 在函数内部,然后调用该函数,但它没有显示任何内容。

I'm currently experimenting with arrays in PHP, and I created a fake environment where a team's information will be displayed.

    $t1 = array (
            "basicInfo" => array (
                "The Sineps",
                "December 25, 2010",
                "lemonpole"
            ),
            "overallRecord" => array (0, 0, 0, 0),
            "overallSeasons" => array (
                1 => array (14, 0, 0),
                2 => array (9, 5, 2),
                3 => array (12, 4, 0),
                4 => array (3, 11, 2)
            ),
            "games" => array (
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />",
                "<img src=\"images/cs.gif\" alt=\"Counter-Strike\" />"
            ),
            "seasonHistory" => array (
                "Season I",
                "Season II",
                "Season III",
                "Season IV"
            ),
            "divisions" => array (
                "Open",
                "Main",
                "Main",
                "Invite"
            )
        );
// Displays the seasons the team has been in along
    // with the record of each season.
    function seasonHistory() {
        // Make array variable local-scope.
        global $t1;

        // Count the number of seasons.
        $numrows = count($t1["seasonHistory"]);

        // Loop through all the variables until
        // it reaches the last entry made and display
        // each item seperately.
        for($v = 0; $v <= $numrows; $v++) {
            // Echo each season.
            echo "<tr><td>{$t1["games"][$v]}</td>";
            echo "<td>{$t1["seasonHistory"][$v]}</td>";
            echo "<td>{$t1["divisions"][$v]}</td></tr>";
        }
    }

I have tested several possible problems out and after narrowing them down I have come down to one conclusion and that is my function is not connecting to the array for some reason. I don't know what else to do because I thought making the array global would fix that problem.

What works:

  1. I can echo $t1["games"][0] on the page I need it to display and it gives me the content.

  2. I tried echo $t1["games"][0] INSIDE the function and then calling the function and it doesn't display anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零度° 2024-10-16 06:51:08

如果您定义的 $t1 不在全局范围内,则可能会发生这种情况。尝试在定义变量后立即执行 $GLOBALS['t1'] = $t1; 显式地将其放入全局变量中。

This can happen if you define your $t1 not in the global scope. Try explicitly putting it into globals, by doing $GLOBALS['t1'] = $t1; right after you define your variable.

沉溺在你眼里的海 2024-10-16 06:51:08
extract($t1);

尝试这个函数而不是全局函数。

更好的主意是将数组作为参数传递......

function seasonHistory($array){

$t1 = $array;

//your function
}
extract($t1);

Try this function instead of global.

Better idea is to pass array as argument...

function seasonHistory($array){

$t1 = $array;

//your function
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文