gpt4 book ai didi

php - 当我打印多维数组时,数组堆叠起来

转载 作者:行者123 更新时间:2023-11-29 05:24:15 25 4
gpt4 key购买 nike

我想制作一个应该像这样打印的数组:

        [0] => 9
[id] => 9
[1] => Barcelona
[name] => Barcelona
[2] => voetbal
[type] => voetbal
[3] => 0
[organisation_id] => 26
[teams] => Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => 2
[tag] => 2
[2] => Barcelona 2
[naam] => Barcelona 2
[3] => 9
[club_id] => 9

[0] => 2
[id] => 2
[1] => 3
[tag] => 3
[2] => Barcelona 3
[naam] => Barcelona 3
[3] => 9
[club_id] => 9
[0] => 9
)

[id] => 10
[1] => Real Madrid
[name] => Real Madrid
[2] => voetbal
[type] => voetbal
[3] => 0
[organisation_id] => 27
[teams] => Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => 2
[tag] => 2
[2] => Real Madrid 2
[naam] => Real Madrid 2
[3] => 9
[club_id] => 10

[0] => 2
[id] => 2
[1] => 3
[tag] => 3
[2] => Real Madrid 3
[naam] => Real Madrid 3
[3] => 10
[club_id] => 10
)

但是我明白了:

[0] => 9
[id] => 9
[1] => Barcelona
[name] => Barcelona
[2] => voetbal
[type] => voetbal
[3] => 0
[organisation_id] => 26
[teams] => Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => 2
[tag] => 2
[2] => Barcelona 2
[naam] => Barcelona 2
[3] => 9
[club_id] => 9

[0] => 2
[id] => 2
[1] => 3
[tag] => 3
[2] => Barcelona 3
[naam] => Barcelona 3
[3] => 9
[club_id] => 9
[0] => 9
)

[id] => 10
[1] => Real Madrid
[name] => Real Madrid
[2] => voetbal
[type] => voetbal
[3] => 0
[organisation_id] => 27
[teams] => Array
(
[0] => Array
(
[0] => 1
[id] => 1
[1] => 2
[tag] => 2
[2] => Barcelona 2
[naam] => Barcelona 2
[3] => 9
[club_id] => 9

[0] => 2
[id] => 2
[1] => 3
[tag] => 3
[2] => Barcelona 3
[naam] => Barcelona 3
[3] => 9
[club_id] => 9
[0] => 9

[0] => 1
[id] => 1
[1] => 2
[tag] => 2
[2] => Real Madrid 2
[naam] => Real Madrid 2
[3] => 10
[club_id] => 10

[0] => 2
[id] => 2
[1] => 3
[tag] => 3
[2] => Real Madrid 3
[naam] => Real Madrid 3
[3] => 10
[club_id] => 10
)

这是我的完整代码:

$resultaat_clubs = mysql_query("SELECT * FROM sportssite_clubs");

$clubs = array();
while($club = mysql_fetch_array($resultaat_clubs)) {
$clubs[] = $club;
}


foreach($clubs as $index => $club) {

$id = $club['id'];
$result = mysql_query("SELECT * FROM eyeboxes WHERE location_id = $id");

if ( mysql_num_rows($result) > 0 ) {
unset($clubs[$index]);
}
}
$teams = array();

foreach($clubs as $index => $club) {
$id = $club['id'];
$result = mysql_query("SELECT * FROM sportssite_teams WHERE club_id = $id");
while($team = mysql_fetch_array($result)) {
// vul de teams array
$teams[] = $team;
}

$clubs[$index]["teams"] = $teams;
}

print_r($clubs);

我不想要答案,而是想要提示之类的东西,当有人给出提示而不是答案时,我学到的更多。但是我找不到错误,我认为秒foreach循环有问题。

提前致谢!

最佳答案

您正在 for..each 循环之外初始化数组 teams。这就是为什么它在一次迭代后没有清除。所以将数组 teams 放入循环中。编辑代码如下:

foreach($clubs as $index => $club) {
$teams = array();
$id = $club['id'];
$result = mysql_query("SELECT * FROM sportssite_teams WHERE club_id = $id");
while($team = mysql_fetch_array($result)) {
// vul de teams array
$teams[] = $team;
}

$clubs[$index]["teams"] = $teams;
}

关于php - 当我打印多维数组时,数组堆叠起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21984017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com