我目前正在尝试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] ,它会给我内容。
我尝试回显 $ t1 [“games”] [0] INSIDE函数然后调用该函数并且它不会显示任何内容。
答案 0 :(得分:3)
如果您将$t1
定义为不在全局范围内,则会发生这种情况。在定义变量后立即执行$GLOBALS['t1'] = $t1;
,尝试将其明确地放入全局变量中。
答案 1 :(得分:2)
extract($t1);
尝试使用此功能而非全局。
更好的想法是将数组作为参数传递......
function seasonHistory($array){
$t1 = $array;
//your function
}