Create an Array Map / Matrix using existing Array of Numbers with Rules / Conditions

NOTE - Please read the information first as it contains important information to understand the problem.

Rules →

• There are 9 Columns (C1,C2,C3,C4,C5,C6,C7,C8,C9) [ Max columns will be 9 ]

• The number of Rows can vary from 3,6,9,12,15,18 (Max). In this case Number of Rows shall be 12

Number of Rows = No of Tickets (Max Allowed 6) x Rows Per Ticket (Max Allowed 3) . Thus, Max Rows can be 18

• Each Row is required to have 4 Blank Spaces and 5 Filled with Numbers

• All numbers available in the Column Array have to be utilized

• This configuration of an shall create a matrix of 9 Columns & 12 Rows ( 3 x 4 Tickets ), which is 108 MATRIX BLOCKS where only a maximum of 60 numbers can be filled out of 108 available blocks randomly with the above conditions being met 100%.

• The numbers in column must be arranged / sorted in ASCENDING ORDER (For coding logic purpose, as soon as the number is assigned to the new MATRIX MAP use array_shift() or unset() the number so as to avoid repetition

Example - Row 1 and Column 1 shall generate a MATRIX BLOCK - R1C1

Row 3 and Column 7 shall generate a MATRIX BLOCK - R3C7

Matrix Block can also be termed as Matrix Cell for your ease (if needed)

MASTER SET OF ARRAY WITH NUMBERS

array( 
    "C1"=> array( 1, 2, 3, 5, 6, 7, 9 ), //7 Numbers
    "C2"=> array( 13, 14, 15, 17, 18, 19 ), //6 Numbers
    "C3"=> array( 21, 22, 23, 24, 25, 26, 30 ), //7 Numbers
    "C4"=> array( 31, 33, 34, 36, 37, 38, 39 ), //7 Numbers
    "C5"=> array( 41, 42, 46, 47, 48, 49, 50 ), //7 Numbers
    "C6"=> array( 51, 52, 53, 54, 55, 57, 58 ), //7 Numbers
    "C7"=> array( 61, 62, 64, 65, 69, 70 ), //6 Numbers
    "C8"=> array( 71, 74, 75, 76, 77, 78 ), //6 Numbers
    "C9"=> array( 82, 83, 85, 87, 88, 89, 90 ) //7 Numbers
    );

The above array has 60 Numbers to be filled out of 108 MATRIX BLOCK / CELL which meets the condition that for a FULL BLOCK containing 4 MINI BLOCKS WITH 3 ROWS (max. allowed) EACH

I have been able to generate this without any issue meeting all the conditions of the Columns

My Allocation Matrix Array will look like

array(
    "R1"=> array(
        "C1"=> true, // Means that MATRIX BLOCK R1C1 will be NOT EMPTY
        "C2"=> false, // Means that MATRIX BLOCK R1C2 will be EMPTY
        "C3"=> true,
        "C4"=> false,
        "C5"=> true,
        "C6"=> false,
        "C7"=> true,
        "C8"=> true,
        "C9"=> false
    ),
    "R2"=> array(
        "C1"=> false,
        "C2"=> true,
        "C3"=> false,
        "C4"=> true,
        "C5"=> false,
        "C6"=> true,
        "C7"=> true,
        "C8"=> true,
        "C9"=> false
    ),
    "R3"=> array(
        "C1"=> true,
        "C2"=> true,
        "C3"=> true,
        "C4"=> true,
        "C5"=> false,
        "C6"=> false,
        "C7"=> false,
        "C8"=> false,
        "C9"=> true
    ),
    "R4"=> array(
        "C1"=> true,
        "C2"=> true,
        "C3"=> true,
        "C4"=> false,
        "C5"=> true,
        "C6"=> true,
        "C7"=> false,
        "C8"=> false,
        "C9"=> false
    ),
    "R5"=> array(
        "C1"=> false,
        "C2"=> false,
        "C3"=> false,
        "C4"=> false,
        "C5"=> true,
        "C6"=> true,
        "C7"=> true,
        "C8"=> true,
        "C9"=> true
    ),
    "R6"=> array(
        "C1"=> true,
        "C2"=> true,
        "C3"=> false,
        "C4"=> true,
        "C5"=> false,
        "C6"=> true,
        "C7"=> false,
        "C8"=> false,
        "C9"=> true
    ),
    "R7"=> array(
        "C1"=> false,
        "C2"=> false,
        "C3"=> true,
        "C4"=> false,
        "C5"=> true,
        "C6"=> false,
        "C7"=> true,
        "C8"=> true,
        "C9"=> true
    ),
    "R8"=> array(
        "C1"=> true,
        "C2"=> false,
        "C3"=> false,
        "C4"=> true,
        "C5"=> false,
        "C6"=> false,
        "C7"=> true,
        "C8"=> true,
        "C9"=> true
    ),
    "R9"=> array(
        "C1"=> true,
        "C2"=> false,
        "C3"=> true,
        "C4"=> false,
        "C5"=> true,
        "C6"=> true,
        "C7"=> false,
        "C8"=> false,
        "C9"=> true
    ),
    "R10"=> array(
        "C1"=> false,
        "C2"=> true,
        "C3"=> true,
        "C4"=> true,
        "C5"=> true,
        "C6"=> false,
        "C7"=> true,
        "C8"=> false,
        "C9"=> false
    ),
    "R11"=> array(
        "C1"=> false,
        "C2"=> true,
        "C3"=> false,
        "C4"=> true,
        "C5"=> true,
        "C6"=> true,
        "C7"=> false,
        "C8"=> true,
        "C9"=> false
    ),
    "R12"=> array(
        "C1"=> true,
        "C2"=> false,
        "C3"=> true,
        "C4"=> true,
        "C5"=> false,
        "C6"=> true,
        "C7"=> false,
        "C8"=> false,
        "C9"=> true
    )
);

In the above array R stands for Row , C for Column , TRUE/FALSE (Boolean) means that if TRUE a Number can be filled in the resulting MATRIX BLOCK / CELL ( Row[Number]Column[Number] ) else if FALSE the MATRIX BLOCK / CELL shall be EMPTY

The result for the above shall beThis is the Manual Allocation done on a Spreadsheet

PROBLEM :

I am unable to understand what should possibly be the logic & loop used here for creating a MATRIX ALLOCATION MAP as shown above

I have tried while, foreach & for but unable determine the perfect combination which would meet the conditions.

(Tried all of the above with Nested Loops also)

Your task would be to keep randomly generating the rows in the Allocation Matrix (inside a loop to limit the maximum number of iterations), until the count of true values in each column is the same as the column count for the master array, i.e. 7 for C1, 6 for C2, … When the counts are the same, you know you have generated an Allocation Matrix that meets all the conditions.

I would recommend that you write a function that generates the Allocation Matrix and a function that calculates the column counts in the result. You can then simply compare an array of the counts from the master array and the array of counts from the Allocation matrix to know when to stop.

Thank you for your response.
I’ve already tried and created the function but the condition is never satisfied, it keeps on running forever.

Someone mentioned putting the code inside of a loop to limit (and detect) when that occurs.

You probably have a logic mistake, which no one can see to help with, since you didn’t post the code that would be needed to reproduce the problem.

Sponsor our Newsletter | Privacy Policy | Terms of Service