Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Subtraction of two Array Matrix in PHP

Subtraction of two Array Matrix in PHP

In this tutorial, we learn the code of subtraction of two array matrix in PHP.

Subtraction of two Array Matrix in PHP with nested For and While Loop

In this tutorial, we learn the code of subtraction of two array matrix with nested for and while loop in PHP.

//sub of matrix ...while loop
<?php
$array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),
'2'=>array('0'=>7,'1'=>8,'2'=>9));
$array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),
'2'=>array('0'=>4,'1'=>8,'2'=>12));

$result=array();
$x=0;
while($x<=2)
{
	for($y=0;$y<=2;$y++)
	{
		$result[$x][$y]=$array1[$x][$y]-$array2[$x][$y];
		
	}
	$x++;
}
	echo "<pre>";
	echo "matrix"." ";
	print_r($result);
	

?>

Output

Subtraction of two Array Matrix in PHP with nested For and While Loop

Figure: Subtraction of two Array Matrix in PHP with nested For and While Loop

Subtraction of two Array Matrix in PHP with nested For Loop

In this tutorial, we learn the code of subtraction of two array matrix with nested for loop in PHP.

//Sub of matrix
<?php
$array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),'2'=>array('0'=>7,'1'=>8,'2'=>9));
$array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),'2'=>array('0'=>4,'1'=>8,'2'=>12));

$result=array();


for($x=0;$x<=2;$x++)
{
	for($y=0;$y<=2;$y++)
	{
		$result[$x][$y]=$array1[$x][$y]-$array2[$x][$y];
		
	}
	echo "<pre>";
	echo "matrix"." ";
	print_r($result);
	//echo $result;
}?>

Output

sub of matrix …do while loop matrixArray ( [0] => Array ( [0] => -1 ) [1] => Array ( [1] => -1 ) [2] => Array ( [2] => -3 ) )

Subtraction of two Array Matrix in PHP with nested For and Do While Loop

In this tutorial, we learn the code of subtraction of two array matrix with nested for and nested do while loop in PHP.

sub of matrix ...do while loop
<?php
$array1=array('0'=>array('0'=>1,'1'=>2,'2'=>3),'1'=>array('0'=>4,'1'=>5,'2'=>6),
'2'=>array('0'=>7,'1'=>8,'2'=>9));
$array2=array('0'=>array('0'=>2,'1'=>4,'2'=>8),'1'=>array('0'=>3,'1'=>6,'2'=>9),
'2'=>array('0'=>4,'1'=>8,'2'=>12));
$result=array();
$x=0;
do
{
	for($y=0;$y<=2;$y++)
	{
		$result[$x][$y]=$array1[$x][$y]-$array2[$x][$y];
		
	$x++;}
	}while($x<=2);
	
	echo "matrix";
	print_r($result);
	//echo $result;</pre>
?>

Subtraction of two Array Matrix in PHP with Form Values entered by the user

In this tutorial, we learn the code of subtraction of two array matrix with form values entered by the user.

//sub of matrix with form 
<html
<body>
<form method="post" action="sub of matrix with form..php">
Array1 <br>
Enter Value: <input type="text" name="1"><br>
Enter Value: <input type="text" name="2"><br>
Enter Value: <input type="text" name="3"><br>
Enter Value: <input type="text" name="4"><br>
Enter Value: <input type="text" name="5"><br>
Enter Value: <input type="text" name="6"><br>
Enter Value: <input type="text" name="7"><br>
Enter Value: <input type="text" name="8"><br>
Enter Value: <input type="text" name="9"><br>

Array2 <br>
Enter Value: <input type="text" name="10"><br>
Enter Value: <input type="text" name="11"><br>
Enter Value: <input type="text" name="12"><br>
Enter Value: <input type="text" name="13"><br>
Enter Value: <input type="text" name="14"><br>
Enter Value: <input type="text" name="15"><br>
Enter Value: <input type="text" name="16"><br>
Enter Value: <input type="text" name="17"><br>
Enter Value: <input type="text" name="18"><br>


<input type="submit"value="sub-of-matrix" name="sub">

</form>

<?php
$array1=array(array(3),array(3));
$array2=array(array(3),array(3));
//$array3=array(array(3),array(3));
if (isset($_POST['sub'])){

$array1[0][0]=$_POST['1'];
$array1[0][1]=$_POST['2'];
$array1[0][2]=$_POST['3'];
$array1[1][0]=$_POST['4'];
$array1[1][1]=$_POST['5'];
$array1[1][2]=$_POST['6'];
$array1[2][0]=$_POST['7'];
$array1[2][1]=$_POST['8'];
$array1[2][2]=$_POST['9'];

$array2[0][0]=$_POST['10'];
$array2[0][1]=$_POST['11'];
$array2[0][2]=$_POST['12'];
$array2[1][0]=$_POST['13'];
$array2[1][1]=$_POST['14'];
$array2[1][2]=$_POST['15'];
$array2[2][0]=$_POST['16'];
$array2[2][1]=$_POST['17'];
$array2[2][2]=$_POST['18'];

$result=array();


for($x=0;$x<=2;$x++)
{
	for($y=0;$y<=2;$y++)
	{
		$result[$x][$y]=$array1[$x][$y]-$array2[$x][$y];
		
	}
	echo "<pre>";
	echo "matrix"." ";
	print_r($result);
	//echo $result;
}}?>
</body>
</html>

Output

Subtraction of two Array Matrix in PHP with Form Values entered by user

Figure: Subtraction of two Array Matrix in PHP with Form Values entered by the user

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.