<?php

	function get_similar_game($title,$num=6){
	    $dir = './sundry/cache/json/games';
	    $file = $dir."/data_{$_GET['k']}.php";
	    if(!is_dir($dir))
	        mk_dir($dir);
	    if(is_file($file)){
	        include $file;
	        $data = json_decode($json,true);
	    }
	    if(empty($data)){
	    	$pdo = get_pdo();
	        $data = $pdo -> query('select title,pic,id,pic2 from '.DB_PRE."z_games_content where class_id='{$_GET['k']}'");
	        $data = empty($data) ? array() : $data;
	        $json = json_encode($data);
	        $json_data = <<<DATA
	<?php
	    \$json = <<<JSON
	        $json
JSON;
	?>
DATA;
			unlink($file);
	        file_put_contents($file,$json_data);
	    }
	    $new_array = getSimilar($title,$data);
	    $new_art_arr = array();
	    foreach($new_array as $k => $v){
	        $new_art_arr[$k] = $v;
	        if(count($new_art_arr)>$num){
	            break;
	        }
	    }

	    return $new_art_arr;
	}
    function getSimilar($title,$arr_title){
        $arr_len = count($arr_title);
        for($i=0; $i<=($arr_len-1); $i++){
            $arr_similar[$i]=similar_text($arr_title[$i]['title'],$title);
        }
        arsort($arr_similar);
        reset($arr_similar);
        $index = 0;
        foreach($arr_similar as $old_index=>$similar){
            $new_title_array[$index] = $arr_title[$old_index];
            $index++;
        }
        return $new_title_array;
    }
?>