[英]Multi dimensional array. What am I doing wrong?
I have a function: 我有一个功能:
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 will be populated with a number of values depending on the $num_players
. Y将根据
$num_players
填充多个值。 I am calling the function as follows: 我调用函数如下:
$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/>";
}
}
As you can see I am doing this wrong. 你可以看到我做错了。 I need to output the data located in
$value
however I am confused with how multi dimensional arrays work. 我需要输出位于
$value
的数据,但是我对多维数组的工作方式感到困惑。 I understand I need to read up on the subject, if possible can you point me to a suitable learning source? 我理解我需要阅读这个主题,如果可能的话,你能指点我一个合适的学习资源吗?
So I changed up the code so that the following is being used to produce an output below: 所以我更改了代码,以便以下内容用于生成以下输出:
$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/>";
}
}
I expected to output as follows: 我期望输出如下:
$value[0]
should contain $y which then contains each SUconsultant value. $value[0]
应该包含$ y,然后包含每个SUconsultant值。 $value[1]
should contain $y which then contains each marketT value and so on. $value[1]
应该包含$ y,然后包含每个marketT值,依此类推。
You have a $j
in your first for loop, but setting and incrimenting $i
in it. 您的第一个for循环中有一个
$j
,但是在其中设置并增加了$i
。 Maybe change them to $j
as well. 也许将它们改为
$j
。
EDIT 编辑
It actually looks like you have an issue with where you are returning the data: 实际上看起来您在返回数据的位置存在问题:
echo "Value = ".$value[$j][$i]."<br/>";
This should be (it appears): 这应该是(出现):
for ($f = 0; $f < $num_players; $f++){
echo "Value = ".$value[$i][$f]."<br/>";
}
Since you are currently on $value[$i]
by the time you reach this point, then you need it to echo out one for each player. 因为当你到达这一点时你目前处于
$value[$i]
,那么你需要它为每个玩家回应一个。
for ($i=0;$j<4;$i++){
should be 应该
for ($j=0;$j<4;$j++){
And 和
$value[$i] = getDecision($num_players, $name[$i], $currentStage)."<br/>";
should be 应该
$value[$j] = getDecision($num_players, $name[$j], $currentStage)."<br/>";
This solution worked, after a lot of playing around it seems that the []
after of the value
array was also needed? 这个解决方案很有效,经过大量的游戏后,似乎还需要
value
数组之后的[]
? At least I think so 至少我是这么认为的
$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[] = getDecision($num_players, $name[$i], $currentStage);
//echo "Value = ".$value[$i]."<br/>";
echo "<br/><hr/>";
echo "Value = ".$value[$i][$j]."<br/><hr/>";
}
}
Output: 输出:
And now I have changed to: 现在我改为:
$value[] = getDecision($num_players, $name[$i], $currentStage);
echo "Team ".$j." ".$name[$i]." = ".$value[$i][$j]."<br/>";
To get my eventual goal of: 为了实现我的最终目标:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.