我该如何排序这个数组

时间:2012-08-05 13:53:43

标签: php arrays sorting

  

可能重复:
  How to sort the results of this code?

我正在制作一个搜索功能,允许用户搜索问题,并通过计算问题中匹配单词的数量来显示前5个最佳匹配结果。 基本上我希望订单首先显示最佳匹配,这将是匹配单词数量最多的问题 这是我的代码。

<?php
include("config.php");
$search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING); //User enetered data
$search_term = str_replace ("?", "", $search_term); //remove any question marks from string
$search_count = str_word_count($search_term);  //count words of string entered by user
$array = explode(" ", $search_term); //Seperate user enterd data



foreach ($array as $key=>$word) {
$array[$key] = " title LIKE '%".$word."%' "; //creates condition for MySQL query
}

$q = "SELECT * FROM posts WHERE  " . implode(' OR ', $array); //Query to select data with word matches
$r = mysql_query($q);
$count = 0; //counter to limit results shown
    while($row = mysql_fetch_assoc($r)){
        $thetitle = $row['title']; //result from query
        $thetitle = str_replace ("?", "", $thetitle);  //remove any question marks from string
        $title_array[] = $thetitle;  //creating array for query results
        $newarray = explode(" ", $search_term); //Seperate user enterd data again
            foreach($title_array as $key => $value) {
                $thenewarray = explode(" ", $value); //Seperate each result from query
                $wordmatch = array_diff_key($thenewarray, array_flip($newarray));
                $result = array_intersect($newarray, $wordmatch);
                $matchingwords = count($result); //Count the number of matching words from
                                                 //user entered data and the database query
            }

if(mysql_num_rows($r)==0)//no result found
    {
    echo "<div id='search-status'>No result found!</div>";
    }
else //result found
    {
    echo "<ul>";
        $title = $row['title'];
        $percentage = '.5'; //percentage to take of search word count
        $percent = $search_count - ($search_count * $percentage); //take percentage off word count
        if ($matchingwords >= $percent){
        $finalarray = array($title => $matchingwords);

        foreach( $finalarray as $thetitle=>$countmatch ){


?>
    <li><a href="#"><?php echo $thetitle ?><i> &nbsp; <br />No. of matching words: <?php echo $countmatch; ?></i></a></li>
<?php


                }
                $count++;
        if ($count == 5) {break;
        }
                }else{



            }
    }
echo "</ul>";
}

&GT?;
当你搜索某些东西时,它会显示这样的东西。
enter image description here
Iv将每个问题下的匹配单词的数量放在一起但是它们没有按顺序排列。它只显示数据库中前50个单词匹配的前5个问题。我希望它以最多匹配单词显示前5名。
我需要添加什么代码以及我将它放在哪里才能执行此操作?
感谢

2 个答案:

答案 0 :(得分:1)

这是我对你的问题的看法。很多事情都发生了变化:

  • mysql_函数已替换为PDO
  • 使用匿名函数意味着需要PHP 5.3
  • 主要逻辑已经重组(很难跟踪你的结果处理,所以我可能会遗漏你需要的东西,例如$percentage

我意识到这可能看起来很复杂,但我认为越早学习现代实践(PDO,匿名功能),你就会越好。

<?php
/**
 * @param string $search_term word or space-separated list of words to search for
 * @param int $count
 * @return stdClass[] array of matching row objects
 */
function find_matches($search_term, $count = 5) {
    $search_term = str_replace("?", "", $search_term);
    $search_term = trim($search_term);
    if(!strlen($search_term)) {
        return array();
    }
    $search_terms = explode(" ", $search_term);

    // build query with bind variables to avoid sql injection
    $params = array();
    $clauses = array();

    foreach ($search_terms as $key => $word) {
        $ident = ":choice" . intval($key);
        $clause = "`title` LIKE {$ident}";
        $clauses []= $clause;
        $params [$ident] = '%' . $word . '%';
    }

    // execute query
    $pdo = new PDO('connection_string');
    $q = "SELECT * FROM `posts` WHERE  " . implode(' OR ', $clauses);
    $query = $pdo->prepare($q);
    $query->execute($params);
    $rows = $query->fetchAll(PDO::FETCH_OBJ);

    // for each row, count matches
    foreach($rows as $row) {
        $the_title = $row->title;
        $the_title = str_replace("?", "", $the_title);

        $title_terms = explode(" ", $the_title);
        $result = array_intersect($search_terms, $title_terms);
        $row->matchcount = count($result);
    }

    // sort all rows by match count descending, rows with more matches come first
    usort($rows, function($row1, $row2) {
        return - ($row1->matchcount - $row2->matchcount);
    });

    return array_slice($rows, 0, $count);
}
?>

<?php
    $search_term = filter_var($_GET["s"], FILTER_SANITIZE_STRING);
    $best_matches = find_matches($search_term, 5);
?>

<?php if(count($best_matches)): ?>
    <ul>
    <?php foreach($best_matches as $match): ?>
        <li><a href="#"><?php echo htmlspecialchars($match->title); ?><i> &nbsp; <br/>No. of matching words: <?php echo $match->matchcount; ?></i></a></li>
    <?php endforeach; ?>
    </ul>
<?php else: ?>
    <div id="search-status">No result found!</div>
<?php endif; ?>

答案 1 :(得分:0)

尝试在asort($finalarray);声明后添加$finalarray = array($title => $matchingwords);

...

if ($matchingwords >= $percent){
    $finalarray = array($title => $matchingwords);
    asort($finalarray);

....

它应该按值增加数组Ascending