Maç sayısına göre sıralama arama sonuçları dizi?

1 Cevap php

Benim site için bir temel arama fonksiyonu inşa ediyorum. En kullanışlı arama verilen terimlerin çoğu sayıları ile sayfasını görüntüleyen gerçekleştiren, ben şu kodlu:

function search()
{
    $this->page = preg_replace('/[^\w \"\']/i', '', trim($this->page));
    preg_match_all('/"(.[^"]+)"|([\w]+)/i', $this->page, $terms);
    $terms=array_unique($terms[0]);  // scrub duplicate search terms
    $tsql="SELECT reviewSummary, reviewFullText, game.gameName FROM review LEFT JOIN game ON review.gameID = game.gameID ";	
    	$constraint="WHERE";
    	foreach ($terms as $term)
    	{
    		$term=str_replace('"','',$term);
    		$tsql.=" $constraint (reviewSummary LIKE '%".$term."%' OR reviewFullText LIKE '%".$term."%' OR game.gameName LIKE '%".$term."%')";
    		$constraint="AND";
    	}
    	$tsql .= "AND game.isPublished = 'y'";
    	$result = $this->sql->db_query($tsql);
    	if (mysql_num_rows($result)!=0)
    	{
    		while($row = mysql_fetch_array($result))
    		{
    			$gameName = stripslashes($row['gameName']);
    			$content = strip_tags($row['reviewFullText']) . ' ' . $row['reviewSummary'];
    			$counter = 0;
    			foreach($terms as $term)
    			{
    				$term=str_replace('"','',$term);
    				preg_match_all("/($term)/i", $content, $matches);
    				foreach($matches[0] as $m)
    				{
    					$counter++;
    				}
    			}
    			$found["Games"][$gameName]=$counter;
    		}	
    	}
    	print_r($found);
}

Harika çalışıyor. Ne doesn't yaptığımız en eşleşmeleri sahiptir göre sipariş sonuçlanır. Ben herkes yardımcı olabilir, bunu başarmak için çıkan dizi sıralamak için nasıl emin değilim?

1 Cevap

uasort() olan bir kullanıcı-tanımlı bir işlev kullanarak bir ilişkisel dizi sıralayabilirsiniz de bir göz atın. Böyle bir şey yapmak gerekir ...

function cmp($a, $b) {
    if ($a == $b) {
        return 0;
    }
    return ($a < $b) ? 1 : -1;
}

uasort($found["Games"], 'cmp');