Venciity

Most Frequent Tag

Aug 15th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 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 method="post" action="">
  9.     <p>Enter tags:</p>
  10.     <input type="text" name="tags" required="true">
  11.     <input type="submit" value="submit" name="submit"/>
  12. </form>
  13. <?php
  14. if (isset($_GET['tags'])) {
  15.     $tags = explode(", ", $_POST['tags']); // Сплитва масива по запетайка и разстояние
  16.     $tagsCountValues = array_count_values($tags); // обхожда масива и връща всяка една стойност колко пъти я има (връща друг масив)
  17.     $maxValue = array_search(max($tagsCountValues), $tagsCountValues); // намираме макса на масива
  18.     arsort($tagsCountValues); // сортираме го по стойност
  19.     foreach ($tagsCountValues as $key => $value) {
  20.         echo "$key : $value times <br>"; // след това го принтим в този ред
  21.     }
  22.     echo "<p>Most Frequent Tag is: $maxValue </p>"; // и изкарваме този таг, който е с най-много повторения
  23. }
  24. ?>
  25. </body>
  26. </html>
Advertisement
Add Comment
Please, Sign In to add comment