AlexKondov

Sort

Aug 15th, 2014
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.76 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>Most Frequent Tag</title>
  6. </head>
  7. <body>
  8.     <form action="" method="post">
  9.         <div>Enter Tags</div>
  10.         <input type="text" name="tags" />
  11.         <input type="submit" />
  12.     </form>
  13.  
  14.     <?php
  15.     if (isset($_POST['tags'])) {
  16.         $arr = explode(", ", $_POST['tags']);
  17.         $tags = [];
  18.         for ($i=0; $i < sizeof($arr); $i++) {
  19.             if (array_key_exists($arr[$i], $tags)) {
  20.                 $tags[$arr[$i]] += 1;
  21.             }
  22.             else {
  23.                 $tags["$arr[$i]"] = 1;
  24.             }
  25.         }
  26.        
  27.         asort($tags);
  28.         $sortedTags = array_reverse($tags);
  29.         foreach ($sortedTags as $key => $value) {
  30.             echo "$key: $value times <br>";
  31.         }
  32.         $val = array_search(max($sortedTags), $sortedTags);
  33.         echo "Most frequent tag is $val";
  34.     }
  35.     ?>
  36. </body>
  37. </html>
Advertisement
Add Comment
Please, Sign In to add comment