We are going to add two matrices using arrays in php. This is so easy and at the end your will recieve results like we have shown the screenshot below. So that you can decide to carry on with this post or move to another.
Matrices
This is how we will add two matrices using php arrays. First you need to make two arrays you wan’t to add. remembers that matrices follow some rules to add. And rules are simple we can have two matrix with the same dimensions if we are going to add them , if we will have matrix one with three column values and matrix two with four column values then addition of matrix for both is not possible. You can read out the complete rules to add two matrices from this link. click here
Make two arrays with the same number of values and add them in the third array. This is how we will add two matrix in the third one. You can get the code that I’ve written to add them.
Code Example:
<center><?php $array1 = array(2,9,2,5,3,2,1,2,1); $array2 = array(2,9,2,5,3,2,1,2,1); echo $array1[0]."   ". $array1[1]."   ". $array1[2]."<br><br>"."   ". $array1[3]."   ". $array1[4]."   ". $array1[5]."   ". "<br><br>". $array1[6]. "   ". $array1[7]. "   ". $array1[8]."<br><br> +" ; echo "<br><br>".$array2[0]."   ". $array2[1]."   ". $array2[2]."<br><br>"."   ". $array2[3]."   ". $array2[4]."   ". $array2[5]."   ". "<br><br>". $array2[6]. "   ". $array2[7]. "   ". $array2[8]."<br><br>"; echo "<hr>"; $array3 = array ($array1[0]+$array2[0], $array1[1]+$array2[1], $array1[2]+$array2[2], $array1[3]+$array2[3], $array1[4]+$array2[4], $array1[5]+$array2[5], $array1[6]+$array2[6], $array1[7]+$array2[7], $array1[8]+$array2[8] ); echo "<h1>Sum Of Two Matrices</h1>"."<br><br>".$array3[0]."   ". $array3[1]."   ". $array3[2]."<br><br>"."   ". $array3[3]."   ". $array3[4]."   ". $array3[5]."   ". "<br><br>". $array3[6]. "   ". $array3[7]. "   ". $array3[8]."<br><br>"; ?>