PHP 数组在函数中不起作用
我目前正在 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>";
}
}
我测试了几个可能的问题,在缩小范围后,我得出了一个结论,那就是我的函数由于某种原因没有连接到数组。我不知道还能做什么,因为我认为使数组全局化可以解决这个问题。
有效方法:
我可以在需要显示的页面上回显$t1["games"][0],它会为我提供内容。
我尝试了 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:
I can echo $t1["games"][0] on the page I need it to display and it gives me the content.
I tried echo $t1["games"][0] INSIDE the function and then calling the function and it doesn't display anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您定义的
$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.尝试这个函数而不是全局函数。
更好的主意是将数组作为参数传递......
Try this function instead of global.
Better idea is to pass array as argument...