Advertisement
em89

Arrays

Jul 5th, 2014
353
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. //1.Print Array
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <title>Print Array</title>
  6. </head>
  7. <body>
  8. <?php
  9. $myArray = array(4, 2, 6, 1, 6, 4, 8, 34, 75, 1, 54, 2, 7, 9, 3, 16, 43, 86, 32, 5);
  10.  
  11. foreach ($myArray as $key => &$value) {
  12. $value = $key*5;
  13. }
  14.  
  15. echo '<pre>' . print_r($myArray, true) . '</pre>';
  16.  
  17. ?>
  18. </body>
  19. </html>
  20.  
  21.  
  22. //2.Largest Sequence of Equal Strings
  23. <!DOCTYPE html>
  24. <html>
  25. <head>
  26. <title>Print Array</title>
  27. </head>
  28. <body>
  29. <form method="post">
  30. <input type="text" name="text">
  31. <input type="submit" value="Go">
  32. </form>
  33. <?php
  34. $myStr = $_POST['text'];
  35. $array = explode(" ", $myStr);
  36. $myArr = array_count_values($array);
  37. foreach ($myArr as $key => $value) {
  38. $value = max($myArr);
  39. $key = array_search($value, $myArr);
  40.  
  41. }
  42. for ($i=1; $i <= max($myArr) ; $i++) {
  43. echo "$key ";
  44. }
  45. ?>
  46. </body>
  47. </html>
  48.  
  49.  
  50. //4.Selection Sort
  51. <!DOCTYPE html>
  52. <html>
  53. <head>
  54. <title>Selection Sort</title>
  55. </head>
  56. <body>
  57. <form method="post">
  58. <input type="text" name="text">
  59. <input type="submit" value="Go">
  60. </form>
  61. <?php
  62. $myStr = $_POST['text'];
  63. $array = explode(" ", $myStr);
  64. $buffer = "";
  65. echo '<pre>' . print_r($array, true) . '</pre>';
  66. for ($i = 0; $i < count($array) - 1; $i++) {
  67. for ($j = $i + 1; $j < count($array); $j++) {
  68. if ((int)$array[$i] > (int)$array[$j]) {
  69. $buffer = $array[$i];
  70. $array[$i] = $array[$j];
  71. $array[$j] = $buffer;
  72. }
  73. }
  74. }
  75. echo '<pre>' . print_r($array, true) . '</pre>';
  76.  
  77. ?>
  78. </body>
  79. </html>
  80.  
  81.  
  82.  
  83. //5.Simple Calculator
  84. <!DOCTYPE html>
  85. <html>
  86. <head>
  87. <title>Selection Sort</title>
  88. </head>
  89. <body>
  90. <form method="POST">
  91. <label for="first">Enter First Number</label>
  92. <input type="text" id="first" name="first">
  93. <br>
  94. <label for="operator">Select Operator</label>
  95. <select name="operator" id="operator">
  96. <option value="plus">+</option>
  97. <option value="minus">-</option>
  98. <option value="multiplication">*</option>
  99. <option value="divide">/</option>
  100. </select>
  101. <br>
  102. <label for="second">Enter Second Number</label>
  103. <input type="text" name="second" id="second">
  104. <br>
  105. <input type="submit" value="Calculate">
  106. </form>
  107. <?php
  108. $first = $_POST['first'];
  109. $second = $_POST['second'];
  110. $operator = $_POST['operator'];
  111. if ($operator == "plus") {
  112. $result = $first + $second;
  113. }
  114. if ($operator == "minus") {
  115. $result = $first - $second;
  116. }
  117. if ($operator == "multiplication") {
  118. $result = $first * $second;
  119. }
  120. if ($operator == "divide") {
  121. $result = $first / $second;
  122. }
  123. echo "Output = $result";
  124. ?>
  125. </body>
  126. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement