简体   繁体   English

PHP 数组对每个元素都有相同的索引

[英]PHP array has the same index for every element

I'm working on a program that prints the qualities of every individual in a team.我正在开发一个程序,该程序可以打印团队中每个人的素质。

I have 3 tables: The Teams table:我有 3 张桌子: Teams 桌子:

TeamID团队ID
1 1
2 2

The TeamPlayers table: TeamPlayers 表:

PlayerID玩家ID Team团队
100 100 1 1
269 269 1 1
357 357 2 2

The program works like this: There is a textbox in the HTML page where the user is typing the ID of the team and the program outputs the members.该程序的工作原理如下: HTML 页面中有一个文本框,用户在其中输入团队的 ID,程序输出成员。 The problem is that each player is stored in an array.问题是每个玩家都存储在一个数组中。 and all have the same index:并且都具有相同的索引:

$fetchPlayersSQL = "SELECT Player_ID
            FROM TeamPlayers
            WHERE Team = $TeamID;";
$fetchPlayers = ibase_query($dbConnection, $fetchPlayersSQL);
while ($row2 = ibase_fetch_object($fetchPlayers)) {
    $fetchPlayersArray = get_object_vars($row2);
    print_r($fetchPlayersArray);
}

Keep in mind $TeamID is the value introduced by the user in the HTML textbox.请记住,$TeamID 是用户在 HTML 文本框中引入的值。

Now, this program outputs this:现在,这个程序输出这个:

Array ( [Player_ID] => 2157 ) 

Array ( [Player_ID] => 734 ) 

Array ( [Player_ID] => 2160 ) 

Array ( [Player_ID] => 3744 ) 

Array ( [Player_ID] => 2166 )

(Keep in mind, 2157, 734, 2160, 3744 and 2166 are other players, there are a lot of them, but I listed only a few) (记住,2157、734、2160、3744和2166是其他玩家,有很多,但我只列出了几个)

The problem is that I need every player to have their own index in the array, because I have to print their qualities问题是我需要每个玩家在数组中都有自己的索引,因为我必须打印他们的素质

I really can't find where the problem comes from.我真的找不到问题出在哪里。 Maybe I am using an incorrect method to select since there are more players in a team.也许我对 select 使用了不正确的方法,因为团队中有更多的玩家。

The 3rd table is just the qualities of every player第三桌只是每个玩家的素质

PlayerID玩家ID Height高度 Weight重量 HairColor发色
100 100 187 187 80 80 black黑色的
357 357 167 167 67 67 grey灰色的
269 269 182 182 95 95 brown棕色的

And the expected output is something like this:而预期的 output 是这样的:

The user enters 1 in the HTML textbox, Team 1 players are 100 and 269 so they should see:用户在 HTML 文本框中输入 1,团队 1 的玩家分别为 100 和 269,因此他们应该看到:

Array ( [0] => 2157 )

Array ( [1] => 734  )

echo $row2->Player_ID; just prints their IDs, we have player 100 and 269 in the Team 1, and this prints 100269只打印他们的 ID,我们在 Team 1 中有玩家 100 和 269,这会打印 100269

It might not be the most elegant solution, but you can always build that array yourself in a loop:它可能不是最优雅的解决方案,但您始终可以自己循环构建该数组:

$a = array();
while ($row2 = ibase_fetch_object($fetchPlayers)) {
    $a[] = $row2->Player_ID;
}
print_r($a);

See if that works for you, I am not able to code at the moment.看看这是否适合你,我目前无法编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM