get-unique-items.php 888 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. // Christopher Pei
  3. // April 12, 2021
  4. if (isset($argv[1])){
  5. if (file_exists($argv[1])){
  6. $input_file = fopen($argv[1], "r") or die("Unable to open file.");
  7. $input = fread($input_file, filesize($argv[1]));
  8. fclose($input_file);
  9. $values = explode(',', $input);
  10. $unique = array();
  11. foreach ($values as $value){
  12. if (!in_array((string)$value, $unique)){
  13. array_push($unique, (string)$value);
  14. }
  15. }
  16. echo "\n Results\n =========================\n";
  17. foreach ($unique as $u){
  18. echo " " . $u . "\n";
  19. }
  20. }
  21. else{
  22. echo " > Sorry, file '" . $argv[1] . "' does not exist.\n";
  23. return 1;
  24. }
  25. }
  26. else{
  27. echo "\n Incorrect Syntax\n =========================\n";
  28. echo " > Missing filename with values.\n";
  29. echo "\n > Usage Example:\n";
  30. echo " > \"php " . $argv[0] . " values.txt\n";
  31. return 1;
  32. }