2

I am currently designing a program that creates a revision timetable for students. I'm trying to create a function that goes through a 2d array and inserts subjects into a random place in that array , ie maths in slot 12 on monday then moves onto the next day.I have both the subjects with assigned number of hours per week set as variables from a previous page I just need help selecting each array within the main array. Here is my 2darray, bear in mind ive only included the first 3 arrays to save space even tho there are 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, is the table filled with values the user put, for example if the user had entered maths for 4 hours english for 6 and biology for 2 the array might look something like 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" => "",
),
);
4
  • Can you provide the expected result? For it is not entirely clear what you want to achieve here. Commented Nov 10, 2016 at 11:03
  • If your subarray goes from 0 to 24, don't you have 25 entries for 24h day? Shouldn't you loose "24" or you have a reason for having two entries for "midnight"? Commented Nov 10, 2016 at 13:09
  • @BozidarSikanjic It should be from 0-23 you are correct thank you Commented Nov 10, 2016 at 13:42
  • do this worked or you need more help ? Commented Dec 22, 2016 at 17:24

1 Answer 1

2

You can do this with one random integer for each level of the array:

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

$string = 'Random Subject'; 

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

Depending on how many 'Subject' you want to add, you should create an array of random Subjects and create another random variable that would select a random string from this array :

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

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

You can repeat this operations as many times as you want, or put it inside a function and repeat it on a for.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for this, sorry for the somewhat broad question and badly formatted code. i'll try to implement this and see if i can get it to work. i'll just need to add some error handling so that 2 subjects are placed in the same time slot but and a loop that runs until all the subjects have been placed in for the correct amount of answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.