如何使用foreach将值添加到数组中的每个数组中?

时间:2016-11-10 10:31:05

标签: php multidimensional-array

我目前正在设计一个程序,为学生创建修订时间表。我正在尝试创建一个通过二维数组的函数,并将主题插入该数组中的随机位置,即星期一的第12个插槽中的数学然后移动到第二天。我有两个主题分配的小时数周设置为上一页的变量我只需要帮助选择主数组中的每个数组。这是我的2darray,请记住,我只包括前3个阵列以节省空间,即使有7个。

$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" => "",
),
);

1 个答案:

答案 0 :(得分: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上重复。