I want to sort values of an array in alphabetical order in PHP. If all values started with same character then they should be sorted using second character and so on. Ignore case sensitive.
For Example:
before:
values[0] = "programming";
values[1] = "Stackoverflow";
values[2] = "question";
values[3] = "answers";
values[4] = "AA Systems";
after:
values[0] = "AA Systems";
values[1] = "answers";
values[2] = "programming";
values[3] = "question";
values[4] = "Stackove开发者_如何学运维rflow";
I have found some algorithms but I want a way that is fast and with small number of statements. Ignoring case sensitive is special for me. Thanks.
See
natcasesort
: http://www.php.net/manual/en/function.natcasesort.phpusort
: http://www.php.net/manual/en/function.usort.php with a comparator function that comparesstrtolower(a)
andstrtolower(b)
.
Your example makes two assumptions:
That you are only dealing with simple, 1-dimensional arrays.
That after sorting alphabetically, your index will update so that the first element alphabetically will be assigned key 0 and so forth.
Given those parameters, your simplest solution is to use the array method sort()
. With your example:
$values[0] = "programming";
$values[1] = "Stackoverflow";
$values[2] = "question";
$values[3] = "answers";
$values[4] = "AA Systems";
sort($values);
Which will result in the following:
Array {
[0] => AA Systems
[1] => Stackoverflow
[2] => answers
[3] => programming
[4] => question
}
There are other array sorting functions that might be a better fit. For instance, the simple one I use above puts upper-case in front of lower-case, so if you had "security" as an item (all lower-case) it would go after "Stackoverflow" since the upper-case s
would take precedence over se
vs. st
. To sort without case-sensitivity, you could use natcasesort()
, which would produce the following with the given array:
Array {
[0] => AA Systems
[1] => answers
[2] => programming
[3] => question
[4] => Stackoverflow
}
As of version 5.4.0, you can just use any of the sort
, asort
, ksort
, etc. functions and pass the SORT_FLAG_CASE
flag.
sort( $array, SORT_FLAG_CASE ); // Non-associative array
asort( $array, SORT_FLAG_CASE ); // Associative array
ksort( $array, SORT_FLAG_CASE ); // Associative array, sort by indices
If you've got an older version and aren't ready to update (or can't), you can use natcasesort
as others have mentioned, but also the uasort
and ksort
variants with strcasecmp
as the custom function:
natcasesort( $array ); // Non-associative array
uasort( $array, 'strcasecmp' ); // Associative array
uksort( $array, 'strcasecmp' ); // Associative array, sort by indices
You can apply the same concept to any of the other sorting functions.
You can use uasort()
: http://php.net/manual/en/function.uasort.php
uasort( $arr, 'strcasecmp' );
The second argument is a function, which compares values. The function must return -1, 0, or 1. Here's a template, which you can use for your custom functions.
function cmp( $a, $b ) {
if ( $a == $b ) return 0;
elseif ( $a > $b ) return 1;
elseif ( $a < $b ) return -1;
}
uasort( $arr, 'cmp' );
After sorting you might want to reset array indexes.
$arr = array_values( $arr );
精彩评论