【发布时间】:2012-02-10 16:14:00
【问题描述】:
我有一个函数:
function getDecision($num_players, $decisionType, $stage){
echo "in the function 1: ".$num_players."<br/>";
echo "in the function 2: ".$decisionType."<br/>";
echo "in the function 3: ".$stage."<br/>";
$x = mysql_query("
SELECT `decisionValue` FROM `teamdecision` WHERE `decisionType` = '$decisionType' && `period`= '$stage'
")or die($x."<br/><br/>".mysql_error());
$y = array();
$i="0";
while ($i<($num_players) && $row = mysql_fetch_assoc($x))
{
$y[$i] = $row['decisionValue'];
$i++;
}
return ($y);
}
Y 将根据$num_players 填充多个值。我调用函数如下:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
如您所见,我做错了。我需要输出位于$value 中的数据,但是我对多维数组的工作方式感到困惑。我知道我需要阅读该主题,如果可能的话,您能否为我指出一个合适的学习资源?
所以我更改了代码,以便使用以下内容生成以下输出:
$value = array();
$name = array();
for ($j=0;$j<$num_players;$j++){
for ($i=0;$i<4;$i++){
$name[0] = "SUconsultant";
$name[1] = "marketT";
$name[2] = "sector";
$name[3] = "saleprice";
echo $name[$i]."<br/>";
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
echo "Value = ".$value[$j][$i]."<br/>";
}
}
我希望输出如下:
$value[0] 应该包含 $y,然后包含每个 SUconsultant 值。 $value[1] 应该包含 $y 然后包含每个 marketT 值等等。
【问题讨论】:
-
IS
for ($i=0;$j<4;$i++)i有故意的吗?因为我觉得不是…… -
好吧,
$i="0";将 $i 定义为字符串 - 它会起作用,但首先不需要引号。 -
对不起,你是对的,我修复了两个评论问题。
标签: php mysql arrays multidimensional-array