2014-02-24 81 views
1

我想讓應印像這樣的數組:當我打印多維數組,數組堆疊起來了

 [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循環的秒數有問題。

在此先感謝!

+0

好像你只想要皇室數據,爲什麼不把它添加到你的查詢中的WHERE子句? –

+0

@ICanHasCheezburger我想打印所有的數字 – STP38

回答

2

您正在初始化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; 
} 
+0

ty來解釋Sherin。 – STP38

+0

很高興我可以幫助.. :) –

1

$teams = array();foreach循環。即。

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; 
} 
+0

它的工作感謝您的時間 – STP38

+0

很高興聽到:),yw –