php - How to use foreach to add values into every array within an array? -
i designing program creates revision timetable students. i'm trying create function goes through 2d array , inserts subjects random place in array , ie maths in slot 12 on monday moves onto next day.i have both subjects assigned number of hours per week set variables previous page need selecting each array within main array. here 2darray, bear in mind ive included first 3 arrays save space tho there 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" => "", ), );
the expected result, table filled values user put, example if user had entered maths 4 hours english 6 , biology 2 array might this.
'$'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" => "", ), );
you can 1 random integer each level of array:
$first = rand(0,6); $second = rand(0,23); $string = 'random subject'; $timetable[$first][$second] = $string;
depending on how many 'subject' want add, should create array of random subjects , create random variable select random string array :
$subjects = ['english', 'maths', 'biology']; $subs = rand(0,count($subjects)); $timetable[$first][$second] = $subjects[$subs];
you can repeat operations many times want, or put inside function , repeat on for
.
Comments
Post a Comment