2016-11-10 74 views
2

我目前正在設計一個程序,爲學生創建修訂時間表。我試圖創建一個函數,通過一個二維數組,並將主題插入到該數組中的隨機位置,即在星期一的第12號槽中的數學運算,然後移動到第二天。我將兩個主題分配給每個小時星期設置爲前一頁的變量我只需要幫助選擇主數組中的每個數組。這裏是我的2darray,牢記香港專業教育學院只包括第3個陣列,以節省空間甚至儘管有7如何使用foreach將值添加到數組中的每個數組中?

$Timetable = array(
"0" => array  // 0 = Monday 6= Sunday 
        // 0 - 24 = horus 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "", 
    "8" => "", 
    "9" => "", 
    "10" => "", 
    "11" => "", 
    "12" => "", 
    "13" => "", 
    "14" => "", 
    "15" => "", 
    "16" => "", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "", 
    "22" => "", 
    "23" => "", 
    "24" => "", 
), 
"1" => array 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "", 
    "8" => "", 
    "9" => "", 
    "10" => "", 
    "11" => "", 
    "12" => "", 
    "13" => "", 
    "14" => "", 
    "15" => "", 
    "16" => "", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "", 
    "22" => "", 
    "23" => "", 
    "24" => "", 
), 
"2" => array 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "", 
    "8" => "", 
    "9" => "", 
    "10" => "", 
    "11" => "", 
    "12" => "", 
    "13" => "", 
    "14" => "", 
    "15" => "", 
    "16" => "", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "", 
    "22" => "", 
    "23" => "", 
    "24" => "", 
), 
); 

預期的結果,就是如果用戶填入值,用戶放,例如表已經進入數學4小時英語6和生物2的數組可能看起來像這樣。

'$'Timetable = array(
"0" => array  // 0 = Monday 6= Sunday 
        // 0 - 24 = horus 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "english", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "maths", 
    "8" => "", 
    "9" => "biology", 
    "10" => "", 
    "11" => "english", 
    "12" => "", 
    "13" => "", 
    "14" => "", 
    "15" => "", 
    "16" => "", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "", 
    "22" => "maths", 
    "23" => "", 
    "24" => "", 
), 
"1" => array 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "", 
    "8" => "", 
    "9" => "english", 
    "10" => "", 
    "11" => "maths", 
    "12" => "", 
    "13" => "", 
    "14" => "", 
    "15" => "", 
    "16" => "english", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "english", 
    "22" => "", 
    "23" => "", 
    "24" => "", 
), 
"2" => array 
(
    "0" => "", 
    "1" => "", 
    "2" => "", 
    "3" => "", 
    "4" => "", 
    "5" => "", 
    "6" => "", 
    "7" => "", 
    "8" => "", 
    "9" => "", 
    "10" => "", 
    "11" => "", 
    "12" => "", 
    "13" => "biology", 
    "14" => "", 
    "15" => "english", 
    "16" => "", 
    "17" => "", 
    "18" => "", 
    "19" => "", 
    "20" => "", 
    "21" => "", 
    "22" => "", 
    "23" => "maths", 
    "24" => "", 
), 
); 
+0

你能提供預期的結果嗎?因爲它不完全清楚你想在這裏實現什麼。 – BVengerov

+0

如果您的子陣列從0到24,您是否有24天的25個條目?你不應該放鬆「24」,或者你有兩個「午夜」條目的理由嗎? –

+0

@BozidarSikanjic它應該從0-23你是正確的謝謝 –

回答

2

可以爲陣列的每個級別和一個隨機整數做到這一點:

$first = rand(0,6); 
$second = rand(0,23); 

$string = 'Random Subject'; 

$timetable[$first][$second] = $string; 

取決於你要多少「主題」來添加,你應該創建隨機人羣的數組,創造條件,從這個數組中選擇一個隨機字符串另一個隨機變量:

$subjects = ['English', 'Maths', 'Biology']; 
$subs = rand(0,count($subjects)); 

$timetable[$first][$second] = $subjects[$subs]; 

,只要你想,你可以重複此操作多次,或把它放在一個函數內,重複上一個for

+1

非常感謝您對此感到抱歉,因爲有些寬泛的問題和格式不正確的代碼。我會盡力實現這一點,看看我能否得到它的工作。我只需要添加一些錯誤處理,以便2個主題被放置在同一個時間段,但是一個循環運行,直到所有主題都被放入正確數量的答案中。 –

相關問題