如何使用PHP对数组进行排序,而忽略开头的文章(a,an,the)? [英] How to sort an array with PHP, ignoring the articles (a, an, the) in the beginning?

查看:50
本文介绍了如何使用PHP对数组进行排序,而忽略开头的文章(a,an,the)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数组$books(实际数组要大得多):

Suppose I have the following array $books (the actual array is much larger):

Array
(
    [0] => The Mystic Masseur
    [1] => The Suffrage of Elvira
    [2] => Miguel Street
    [3] => A House for Mr Biswas
    [4] => Mr Stone and the Knights Companion
    [5] => The Mimic Men
    [6] => A Flag on the Island
    [7] => In a Free State
    [8] => Guerrillas
    [9] => A Bend in the River
    [10] => The Enigma of Arrival
    [11] => A Way in the World
    [12] => Half a Life
    [13] => Magic Seeds
)

当我使用sort函数时,结果是这样的:

When I use the sort function, the result is this:

Array
(
    [0] => A Bend in the River
    [1] => A Flag on the Island
    [2] => A House for Mr Biswas
    [3] => A Way in the World
    [4] => Guerrillas
    [5] => Half a Life
    [6] => In a Free State
    [7] => Magic Seeds
    [8] => Miguel Street
    [9] => Mr Stone and the Knights Companion
    [10] => The Enigma of Arrival
    [11] => The Mimic Men
    [12] => The Mystic Masseur
    [13] => The Suffrage of Elvira
)

但是我想忽略开头的文章(a,an,the),并希望得到这样的结果:

But I would like to ignore the articles (a, an, the) in the beginning and would like to have a result like this:

Array
(
    [0] => A Bend in the River
    [1] => The Enigma of Arrival
    [2] => A Flag on the Island
    [3] => Guerrillas
    [4] => Half a Life
    [5] => A House for Mr Biswas
    [6] => In a Free State
    [7] => Magic Seeds
    [8] => Miguel Street
    [9] => The Mimic Men
    [10] => Mr Stone and the Knights Companion
    [11] => The Mystic Masseur
    [12] => The Suffrage of Elvira
    [13] => A Way in the World
)

我尝试过:

$_books = array();
foreach($books as $book){
    if(substr($book, 0, 4) == "The "){
    $_books[substr($book, 4)] = $book;
    }
    else if(substr($book, 0, 3) == "An "){
    $_books[substr($book, 3)] = $book;
    }
    else if(substr($book, 0, 2) == "A "){
    $_books[substr($book, 2)] = $book;
    }
    else{
    $_books[$book] = $book;
    }
}
ksort($_books);
$books = array_values($_books);

但是我知道这不是最好的解决方案,因为它有失去价值的风险(例如,如果输入中同时存在"A Fantasy"和"The Fantasy",则输出中将只有"The Fantasy" ).那么,最好的解决方案是什么?

But I know this is not the best solution because it has the risk of losing values (e.g. if there are both "A Fantasy" and "The Fantasy" in the input, there will be only "The Fantasy" in the output). So, what is the best solution?

推荐答案

使用自定义排序功能:

function handleArticles($str) {
    list($first,$rest) = explode(" ",$str." ",2);
       // the extra space is to prevent "undefined offset" notices
       // on single-word titles
    $validarticles = array("a","an","the");
    if( in_array(strtolower($first),$validarticles)) return $rest.", ".$first;
    return $str;
}
usort($books,function($a,$b) {
    return strnatcasecmp(handleArticles($a),handleArticles($b));
});

这篇关于如何使用PHP对数组进行排序,而忽略开头的文章(a,an,the)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆