2012-09-28 44 views
-4

讓我解釋一下數組波紋管:PHP數組,獲取循環索引信息

1,2和3代表星期一,星期二和星期三。

13,14和15是月的日子。

和15:00 - 16:20(..)是電影將要顯示的時間。

含義13日星期一從下午3點到下午4:20你可以看「蝙蝠俠」。

同樣在13日星期一下午6點10分到7點30分,你可以看「苦難」......等等。

$myarray 
: array = 
    1: array = 
    13: array = 
     15:00 - 16:20: array = 
     Movie: string = Batman     
     18:10 - 19:30: array = 
     Movie: string = Misery     
    2: array = 
    14: array = 
     15:00 - 16:20: array = 
     Movie: string = The Avengers     
     16:20 - 17:50: array = 
     Movie: string = The Hours     
    3: array = 
    15: array = 
     18:10 - 19:30: array = 
     Movie: string = Gladiator 

能否請你幫我回音此陣,所以你可以看到它的信息按以下格式?:

Monday - 13 
15:00 - 16:20 - Batman 
18:10 - 19:30 - Misery 
Tuesday – 14 
15:00 - 16:20 - The Avengers     
16:20 - 17:50 - The Hours 
Wednesday – 15 
8:10 - 19:30 - Gladiator 

非常感謝!

+3

你的陣列是無效的PHP的格式。 '16:20 - 17:50'應該是關鍵嗎?即你可以用'$ myarray [1] [13] ['15:00 - 16:20']'來獲取array(「Movie」=>「Batman」)'(我不希望!我很抱歉,我不明白你使用的符號。 –

+0

你可以添加確切的數組 – Baba

回答

0

我對數組的真實佈局做了一個假設,下面將創建一個$weekDays數組來映射一個整數和一週中的某一天(我定義了鍵,以便您可以隨時移動它們):

$weekDays = (1=>'Monday', 2=>'Tuesday', 3=>'Wednesday', 4=>'Thursday', 5=>'Friday', 6=>'Saturday', 7=>'Sunday'); 

// loop through each week-day in the array 
foreach ($myarray as $weekDay => $dates) { 
    // loop through each "movie date" in the current week-day block 
    foreach ($dates as $monthDate => $movies) { 
     // output the day-of-the-week and the date-of-the-month 
     echo $weekDays[$weekDay] . ' - ' . $monthDate . '<br />'; 

     // loop through all of the movies for the current date-of-the-month 
     foreach ($movies as $movieTime => $movie) { 
      // output the movie's time and name 
      echo $movieTime . ' - ' . $movie . '<br />'; 
     } 
    } 
} 

這是假設在你的榜樣陣列真的格式爲:

$myarray = array(
    1 => array(
     13 => array(
      '15:00 - 16:20' => array(
       'Movie' => 'Batman' 
      ), 
      '18:10 - 19:30' => array(
       'Movie' => 'Misery' 
      ) 
     ), // ... can repeat 
    ), // ... can repeat 
); 
+0

謝謝! – user523129

+0

@ user523129沒問題,很高興它的作品! – newfurniturey

0

你可以試試下面的下面的代碼:See Demo

$myarray = array(
    1 => array(
     13 => array(
      "15:00 - 16:20" => array(
       "Movie" => " Batman" 
      ), 
      "18:10 - 19:30" => array(
       "Movie" => " Misery" 
      ) 
     ) 
    ), 
    2 => array(
     14 => array(
      "15:00 - 16:20" => array(
       "Movie" => " The Avengers " 
      ), 
      "16:20 - 17:50" => array(
       "Movie" => " The Hours " 
      ) 
     ) 
    ), 
    3 => array(
     15 => array(
      "18:10 - 19:30" => array(
       "Movie" => " Gladiator" 
      ) 
     ) 
    ) 
); 


$days = array(1 => 'Monday',2 => 'Tuesday',3 => 'Wednesday'); 

echo "<pre>"; 
foreach ($myarray as $key => $value) { 
    foreach ($value as $valueKey => $info) { 
     echo $days[$key], " - ", $valueKey, PHP_EOL; 
     foreach ($info as $infoKey => $infoValue) { 
      echo $infoKey, " - ", $infoValue['Movie'], PHP_EOL; 
     } 
     echo PHP_EOL; 
    } 
} 

輸出

Monday - 13 
15:00 - 16:20 - Batman 
18:10 - 19:30 - Misery 

Tuesday - 14 
15:00 - 16:20 - The Avengers 
16:20 - 17:50 - The Hours 

Wednesday - 15 
18:10 - 19:30 - Gladiator