Addition of Two Matrices in PHP

Given two matrices mat1 and mat2, the task is to find the sum of both matrices. Matrix addition is possible only when both matrix’s dimensions are the same. If both matrices have different dimensions then addition will not be possible.

Table of Content

  • Sum of Two Matrices using for Loop
  • Using array_map with Anonymous Function

Sum of Two Matrices using for Loop

First, we declare two matrices, matrix1 and matrix2. Next, we check both matrix dimension, if both matrix dimension is same then we proceed for sum of matrices, otherwise display an error message. To add both matrices, we use for loop. For loop iterate each matrix elements one by one, and perform sum on the elements.

Example: Sum of two matrices using for Loop in PHP.

PHP
<?php 

function sumTwoMatrices($mat1, $mat2) { 
    $rows = count($mat1); 
    $columns = count($mat1[0]); 

    // Sum Matrix Initialize with Zeros 
    $sum = array(); 
    for ($i = 0; $i < $rows; $i++) { 
        for ($j = 0; $j < $columns; $j++) { 
            $sum[$i][$j] = 0; 
        } 
    } 

    // Matrix Addition 
    for ($i = 0; $i < $rows; $i++) { 
        for ($j = 0; $j < $columns; $j++) { 
            $sum[$i][$j] = $mat1[$i][$j] + $mat2[$i][$j]; 
        } 
    } 

    return $sum; 
} 

// First Matrix 
$matrix1 = array( 
    array(26, 14, 31), 
    array(41, 61, 11), 
    array(14, 12, 19) 
); 

// Second Matrix 
$matrix2 = array( 
    array(11, 8, 27), 
    array(21, 15, 25), 
    array(22, 15, 21) 
); 

// Get the first Matrix dimension 
$rows1 = count($matrix1); 
$columns1 = count($matrix1[0]); 

// Get the second Matrix dimension 
$rows2 = count($matrix2); 
$columns2 = count($matrix2[0]); 

// Check both matrix dimension is equal or not 
if($rows1 != $rows2 || $columns1 != $columns2) { 
    echo "Matrix dimension not Equal. Sum not possible"; 
} 
else { 
    
    // Sum of Matrices 
    $sum = sumTwoMatrices($matrix1, $matrix2); 

    // Print the Sum Matrix 
    foreach ($sum as $row) { 
        echo implode("\t", $row) . "\n"; 
    } 
} 

?>

Output
37    22    58
62    76    36
36    27    40

Using array_map with Anonymous Function

You can add two matrices in PHP using array_map with an anonymous function. This approach applies the addition operation element-wise to corresponding elements of the matrices, resulting in a new matrix with the sums.

Example : This PHP script computes and prints the sum of two matrices, ensuring dimensions match. Outputs formatted matrix sum for console display using `implode` and `\n`.

PHP
<?php

function sumTwoMatrices($mat1, $mat2) {
    return array_map(function($row1, $row2) {
        return array_map(function($elem1, $elem2) {
            return $elem1 + $elem2;
        }, $row1, $row2);
    }, $mat1, $mat2);
}

// First Matrix
$matrix1 = [
    [26, 14, 31],
    [41, 61, 11],
    [14, 12, 19]
];

// Second Matrix
$matrix2 = [
    [11, 8, 27],
    [21, 15, 25],
    [22, 15, 21]
];

// Get the first Matrix dimension
$rows1 = count($matrix1);
$columns1 = count($matrix1[0]);

// Get the second Matrix dimension
$rows2 = count($matrix2);
$columns2 = count($matrix2[0]);

// Check both matrix dimensions are equal or not
if ($rows1 != $rows2 || $columns1 != $columns2) {
    echo "Matrix dimensions are not equal. Sum is not possible.\n";
} else {
    // Sum of Matrices
    $sum = sumTwoMatrices($matrix1, $matrix2);

    // Print the Sum Matrix for console output
    foreach ($sum as $row) {
        echo implode("\t", $row) . "\n";
    }
}

?>

Output
37    22    58
62    76    36
36    27    40