PHP Program to check whether the two matrices are equal or not

How to check whether the two matrices are equal or not?

To check whether the two matrices are equal or not first we have to find the number of rows and columns in the array and compare the corresponding index together. For example, If the arrays are

        1  3  1              1  3  1

a[] = 3  1  1     b[] =  3  1  1

        3  1  2               3  1  2

in this case, the matrices will be equal.

How to check whether the two matrices are equal or not using PHP?

In this program, we are using the static values which are initialized in the code itself. First, we have to initialize 2 two-dimensional arrays a1 and a2 we have to assign the number of rows and columns into the variable row and col using the built-in function count(). After that, we have to assign the value true to the variable flag. Then assign the value 0 into the variable i and perform the loop until the condition 'i < row' becomes false and increment the value of variable i in every iteration in the block of the loop we have to perform another loop in that we have to assign the value 0 into the variable j and perform the loop until the condition 'j < col' becomes false and increment the value of variable j in every iteration in the loop block we have to check the condition 'a1[i][j] != a2[i][j]' if true assign the value false into the variable flag. After the completion of the loop, we have to check the condition if the flag is true print Matrices are equal otherwise print Matrices are not equal.

ALGORITHM

Step 1: Initialize 2 two-dimensional arrays a1[] and a2[]

Step 2: Assign the number of rows and columns into the variable row and col using the built-in function count()

Step 3: Print the elements in the array a1[] and a2[]

Step 4: Create a temporary variable flag with the value true

Step 5: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < row' becomes false and increment the value of variable i in every iteration

        (i) Assign the value 0 into the variable j and perform the sub-step until the condition 'j < col' becomes false and increment the value of variable j in                every iteration

            (a) Check the condition 'a1[i][j] != a2[i][j]' if true assign the value false to the variable flag

Step 6: Check the condition if the flag is true print Matrices are equal otherwise print Matrices are not equal

PHP Source Code

                                          <?php
$a1 = array(
    array(6, 7, 5),
    array(3, 5, 1),
    array(8, 3, 4)
);
$a2 = array(
    array(6, 7, 5),
    array(3, 5, 1),
    array(8, 3, 4)
);
$row = count($a1);
$col = count($a1[0]);
echo "First matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a1[$i][$j] . " ";
    }
    echo "\n";
}
echo "Second matrix: \n";
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        echo $a2[$i][$j] . " ";
    }
    echo "\n";
}
$flag = true;
for ($i = 0; $i < $row; $i++) {
    for ($j = 0; $j < $col; $j++) {
        if ($a1[$i][$j] != $a2[$i][$j]) {
            $flag = false;
            break;
        }
    }
}
if ($flag)
    echo "Matrices are equal";
else
    echo "Matrices are not equal";
                                      

OUTPUT

First matrix:
6 7 5
3 5 1
8 3 4
Second matrix:
6 7 5
3 5 1
8 3 4
Matrices are equal