get-unique-items.php 901 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. return 0;
  21. }
  22. else{
  23. echo " > Sorry, file '" . $argv[1] . "' does not exist.\n";
  24. return 1;
  25. }
  26. }
  27. else{
  28. echo "\n Incorrect Syntax\n =========================\n";
  29. echo " > Missing filename with values.\n";
  30. echo "\n > Usage Example:\n";
  31. echo " > \"php " . $argv[0] . " values.txt\n";
  32. return 1;
  33. }