fork download
  1. <?php
  2.  
  3. Class Kamus {
  4. private $list = [];
  5.  
  6. public function tambah($kata, $sinonim) {
  7. $semua = array_unique(array_merge([$kata], $sinonim));
  8. $gabungan = [];
  9.  
  10. foreach ($semua as $k) {
  11. if (isset($this->list[$k])) {
  12. $gabungan = array_merge($gabungan, $this->list[$k], [$k]);
  13. } else {
  14. $gabungan[] = $k;
  15. }
  16. }
  17.  
  18. $gabungan = array_unique($gabungan);
  19.  
  20. foreach ($gabungan as $k) {
  21. $this->list[$k] = array_values(array_diff($gabungan, [$k]));
  22. }
  23. }
  24.  
  25. public function ambilSinonim($kata){
  26. return $this->list[$kata] ?? null;
  27. }
  28. }
  29.  
  30. $kamus = new Kamus();
  31. $kamus->tambah('tua', ['berumur', 'sepuh']);
  32. $kamus->tambah('tua', ['baya', 'jompo']);
  33.  
  34. print_r($kamus->ambilSinonim('tua'));
  35. print_r($kamus->ambilSinonim('jompo'));
Success #stdin #stdout 0.03s 25692KB
stdin
Standard input is empty
stdout
Array
(
    [0] => berumur
    [1] => sepuh
    [2] => baya
    [3] => jompo
)
Array
(
    [0] => berumur
    [1] => sepuh
    [2] => tua
    [3] => baya
)