fork download
  1. <?php
  2.  
  3. class Kamus {
  4. private $data = [];
  5.  
  6. public function tambah($kata, $sinonim) {
  7. $dataKata = isset($this->data[$kata]) ? $this->data[$kata] : [];
  8.  
  9. foreach($sinonim as $s) {
  10. if(!in_array($s, $dataKata)) {
  11. $dataKata[] = $s;
  12. }
  13. }
  14.  
  15. $this->data[$kata] = $dataKata;
  16. }
  17.  
  18. public function ambilSinonim($kata) {
  19. if(isset($this->data[$kata])) {
  20. return $this->data[$kata];
  21. }
  22.  
  23. foreach($this->data as $key => $sinonim) {
  24. if(in_array($kata, $sinonim)) {
  25. return array_merge([$key], $sinonim);
  26. }
  27. }
  28.  
  29. return null;
  30. }
  31. }
  32.  
  33. $kamus = new Kamus();
  34. $kamus->tambah('big', ['large', 'great']);
  35. $kamus->tambah('big', ['huge', 'fat']);
  36. $kamus->tambah('huge', ['enormous', 'gigantic']);
  37.  
  38. echo $kamus->ambilSinonim('big');
  39. echo $kamus->ambilSinonim('huge');
  40. echo $kamus->ambilSinonim('gigantic');
  41. echo $kamus->ambilSinonim('colossal');
Success #stdin #stdout #stderr 0.03s 25392KB
stdin
Standard input is empty
stdout
ArrayArrayArray
stderr
PHP Notice:  Array to string conversion in /home/9AHhsh/prog.php on line 38
PHP Notice:  Array to string conversion in /home/9AHhsh/prog.php on line 39
PHP Notice:  Array to string conversion in /home/9AHhsh/prog.php on line 40