$t_r) { return -1; } elseif($t_l == $t_r) { $c_l = intval($left['count']); $c_r = intval($right['count']); if($c_l > $c_r) { return 1; } elseif($c_l == $c_r) { return 0; } else { return -1; } } else { return 1; } } class history_helper { private $mContainer; public function __construct() { if(isset($_SESSION['search_history'])) { $histories = $_SESSION['search_history']; } else { $histories = []; } $this->mContainer = []; foreach ($histories as $value) { $keyword = $value['keyword']; $this->mContainer[$keyword] = $value; } } public function __destruct() { $history = []; foreach ($this->mContainer as $key => $value) { $history[] = $value; } $_SESSION['search_history'] = $history; } public function add_word($keyword) { if(empty($keyword)) { return false; } if(isset($this->mContainer[$keyword])) { $this->mContainer[$keyword]['add_time'] = time(); $this->mContainer[$keyword]['count'] += 1; } else { $this->mContainer[$keyword]['add_time'] = time(); $this->mContainer[$keyword]['count'] = 1; $this->mContainer[$keyword]['keyword'] = $keyword; } uasort($this->mContainer,'comp_history'); return true; } public function histories() { $pos = 0; $result = []; foreach ($this->mContainer as $key => $value) { $result[] = $value['keyword']; if($pos > 10) { break; } ++$pos; } return $result; } }