Finding the Highest Lowest value in an array – PHP MySQL
In this tutorial, we will cover the highest and lowest value program in PHP and MySQL.
Finding the highest lowest value in an array using while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> </html> <?php $t4_mtx = array ( array(1,8,4), array(2,9,5), array(3,6,10) ); $i = 0; while($i < 3) { $j = 0; while($j < 3) { echo $t4_mtx[$i][$j]." "; $j++; } echo "<br>"; $i++; } $t4_high=$t4_mtx[0][0]; $t4_low=$t4_mtx[0][0]; $i=0; while($i<3) { $j=0; while($j<3) { if($t4_mtx[$i][$j] > $t4_high) $t4_high=$t4_mtx[$i][$j]; $j++; } $i++; } $i=0; while($i<3) { $j=0; while($j<3) { if($t4_mtx[$i][$j] < $t4_low) $t4_low=$t4_mtx[$i][$j]; $j++; } $i++; } echo "Highest value=".$t4_high; echo "<br/>"; echo "lowest value=".$t4_low; ?> |
Output
Finding the highest lowest value in an array using do while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> </head> <body> </body> </html> <?php $t4_mtx = array ( array(1,8,4), array(2,9,5), array(3,6,10) ); $i = 0; do { $j = 0; while($j < 3) { echo $t4_mtx[$i][$j]." "; $j++; } echo "<br>"; $i++; }while($i < 3); $t4_high=$t4_mtx[0][0]; $t4_low=$t4_mtx[0][0]; $i=0; Do { $j=0; Do { if($t4_mtx[$i][$j] > $t4_high) $t4_high=
$t4_mtx[$i][$j]; $j++; }while($j<3); $i++; }while($i<3); $i=0; Do { $j=0; Do { if($t4_mtx[$i][$j] < $t4_low) $t4_low=$t4_mtx[$i][$j]; $j++; }while($j<3); $i++; }while($i<3); echo "Highest value=".$t4_high; echo "<br/>"; echo "lowest value=".$t4_low; ?> |
Finding the highest Lowest value in an array value entered by the user using while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style> input[type="text"] { width: 20px; height: 20px; } </style> </head> <body> <form method ="Post"> Matrix <br> <input type="text" name="1"> <input type="text" name="2"> <input type="text" name="3"><br> <input type="text" name="4"> <input type="text" name="5"> <input type="text" name="6"><br> <input type="text" name="7"> <input type="text" name="8"> <input type="text" name="9"><br> <input type ="submit" name="ins" value="Insert"> </form> </body> </html> <?php $t4_mtx = array(array(3), array(3)); if (isset($_POST['ins'])) { $t4_mtx[0][0]=$_POST['1']; $t4_mtx[0][1]=$_POST['2']; $t4_mtx[0][2]=$_POST['3']; $t4_mtx[1][0]=$_POST['4']; $t4_mtx[1][1]=$_POST['5']; $t4_mtx[1][2]=$_POST['6']; $t4_mtx[2][0]=$_POST['7']; $t4_mtx[2][1]=$_POST['8']; $t4_mtx[2][2]=$_POST['9']; $i = 0; while($i < 3) { $j = 0; while($j < 3) { echo $t4_mtx[$i][$j]." "; $j++; } echo "<br>"; $i++; } $t4_low=$t4_mtx[0][0]; $t4_high=$t4_mtx[0][0]; $i=0; while($i<3) { $j=0; while($j<3) { if($t4_mtx[$i][$j] > $t4_high) $t4_high=$t4_mtx[$i][$j]; $j++; } $i++; } $i=0; while($i<3) { $j=0; while($j<3) { if($t4_mtx[$i][$j] < $t4_low) $t4_low=$t4_mtx[$i][$j]; $j++; } $i++; } echo "Highest value=".$t4_high; echo "<br/>"; echo "lowest value=".$t4_low; } ?> |
Output
Finding the highest Lowest value in an array value entered by the user using do while loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style> input[type="text"] { width: 20px; height: 20px; } </style> </head> <body> <form method ="Post"> Matrix <br> <input type="text" name="1"> <input type="text" name="2"> <input type="text" name="3"><br> <input type="text" name="4"> <input type="text" name="5"> <input type="text" name="6"><br> <input type="text" name="7"> <input type="text" name="8"> <input type="text" name="9"><br> <input type ="submit" name="ins" value="Insert"> </form> </body> </html> <?php $t4_mtx = array(array(3), array(3)); if (isset($_POST['ins'])) { $t4_mtx[0][0]=$_POST['1']; $t4_mtx[0][1]=$_POST['2']; $t4_mtx[0][2]=$_POST['3']; $t4_mtx[1][0]=$_POST['4']; $t4_mtx[1][1]=$_POST['5']; $t4_mtx[1][2]=$_POST['6']; $t4_mtx[2][0]=$_POST['7']; $t4_mtx[2][1]=$_POST['8']; $t4_mtx[2][2]=$_POST['9']; $i = 0; Do { $j = 0; Do { echo $t4_mtx[$i][$j]." "; $j++; }while($j < 3); echo "<br>"; $i++; }while($i < 3); $t4_low=$t4_mtx[0][0]; $t4_high=$t4_mtx[0][0]; $i=0; Do { $j=0; Do { if($t4_mtx[$i][$j] > $t4_high) $t4_high=$t4_mtx[$i][$j]; $j++; }while($j<3); $i++; }while($i<3); $i=0; while($i<3) { $j=0; while($j<3) { if($t4_mtx[$i][$j] < $t4_low) $t4_low=$t4_mtx[$i][$j]; $j++; } $i++; } echo "Highest value=".$t4_high; echo "<br/>"; echo "lowest value=".$t4_low; } ?> |