PHP array_multisort two arrays sort by column example

A common problem we run into in day to day PHP programming is to sort two arrays by one column. Especially when the data comes from database. We sometimes need to write code to do the things that a simple SQL will do it. Actually we are simulating a SQL statement like this

 
SELECT a, b FROM TABLE ORDER BY a DESC;
 

SQL is a declaration language and good at such tasks, PHP is an imperative language, to do the same thing we need to give the detailed steps to tell computer to do the same thing. Thats the reason why language like C#.NET introduce LINQ to alleviate the burden on programmers. There are several ported LINQ for PHP, check them out if you want to save your precious time.

In this example, we have a list of strings, they are article titles, we will calculate the similarity scores between each of them and a provided string. We want to find the first 10 most related posts of the current title.

The function we uses to calculate similarity is similar_text

 
function string_similarity ($a, $b) {
    return similar_text($a, $b);
}
 

Here is the function to get the top 10 similar posts

 
function get_similar_titles($anchor, $candidates){
    $similar_scores;
    foreach($candidates as $title) {
        $similar_scores[] = string_similarity($anchor, $title[0]);
    }
    // print_r($similar_scores);
 
    array_multisort($similar_scores, SORT_DESC, $candidates);
 
    return array_slice($candidates, 0 , 10);
}
 

In SQL it will be much simpler:

 
SELECT title, url  FROM TABLE ORDER BY score DESC LIMIT 10;
 

To test this function, we feed some data to the function

 
function test_get_similar_titles(){
  $anchor = "Gradle Slf4j integration example with Eclipse";
  $candidates = array(
    array("Copy files from multiple locations to one destination with Gradle copy task","http://makble.com/copy-files-from-multiple-locations-to-one-destination-with-gradle-copy-task"),
    array("Gradle build in Eclipse: Can not find tools.jar","http://makble.com/gradle-build-in-eclipse-can-not-find-toolsjar"),
    array("PHP install PEAR and mongodb on windows","http://makble.com/php-install-pear-and-mongodb-on-windows"),
    array("Gradle configure selected subprojects","http://makble.com/gradle-configure-selected-subprojects"),
    array("Advantages of Gradle compared to Ant and Maven","http://makble.com/advantages-of-gradle-compared-to-and-and-maven"),
    array("Java and Groovy can not pass parameter by reference","http://makble.com/java-and-groovy-can-not-pass-parameter-by-reference"),
    array("Automatic generate id for Solr document","http://makble.com/automatic-generate-id-for-solr-document"),
    array("How to config Message Resources in Struts","http://makble.com/how-to-config-message-resources-in-struts"),
    array("Using PHP retrieve Solr's search result","http://makble.com/using-php-retrieve-solrs-search-result"),
    array("Gradle build heap space problem with findbugs plugin","http://makble.com/gradle-build-heap-space-problem-with-findbugs-plugin"),
    array("How to change Color Theme in Sublime quickly","http://makble.com/how-to-change-color-theme-in-sublime-quickly"),
    array("Spring Data JPA, Spring MVC and Gradle integration","http://makble.com/spring-data-jpa-spring-mvc-and-gradle-integration"),
    array("Lucene field boost example","http://makble.com/lucene-field-boost-example"));
 
  print_r(get_similar_titles($anchor, $candidates));
}