多维数组。我究竟做错了什么?

时间:2022-09-20 19:06:15

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. I am calling the function as follows:

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/>";
}
}

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. I understand I need to read up on the subject, if possible can you point me to a suitable learning source?

如你所见,我做错了。我需要输出位于$ value中的数据,但是我对多维数组的工作原理感到困惑。我理解我需要阅读这个主题,如果可能的话,你能指出我一个合适的学习资源吗?

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[1] should contain $y which then contains each marketT value and so on.

$ value [0]应包含$ y,然后包含每个SUconsultant值。 $ value [1]应该包含$ y,然后包含每个marketT值,依此类推。

3 个解决方案

#1


1  

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/>";

#2


1  

You have a $j in your first for loop, but setting and incrimenting $i in it. Maybe change them to $j as well.

你的第一个for循环中有一个$ j,但设置并且在其中加入$ i。也许将它们改为$ 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],所以你需要它为每个玩家回应一个。

#3


1  

This solution worked, after a lot of playing around it seems that the [] after of the value array was also needed? 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:

为了实现我的最终目标:

多维数组。我究竟做错了什么?

#1


1  

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/>";

#2


1  

You have a $j in your first for loop, but setting and incrimenting $i in it. Maybe change them to $j as well.

你的第一个for循环中有一个$ j,但设置并且在其中加入$ i。也许将它们改为$ 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],所以你需要它为每个玩家回应一个。

#3


1  

This solution worked, after a lot of playing around it seems that the [] after of the value array was also needed? 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:

为了实现我的最终目标:

多维数组。我究竟做错了什么?