Reputation: 10649
I am trying to sort an array by the length of characters in each value (and perhaps, if possible, in alphabetical order if two values have the same length of characters). For example:
Array ( [0] => this [1] => is [2] => a [3] => bunch [4] => of [5] => words;
I am trying to sort this array to look like:
Array ( [0] => a [1] => is [2] => of [3] => this [4] => bunch [5] => words;
How?
Upvotes: 5
Views: 10234
Reputation: 12333
See this:
$myarray = explode(" ", "this is a bunch of words");
usort($myarray, function($a, $b) { return strlen($a) - strlen($b) });
print_r($array);
Explanation: usort
sorts in place (i.e. by reference) an array, given a custom comparator. A comparator must return a negative number if the first item should be before the second item, and a positive otherwise. A value of 0 means they are equal regarding the order.
You can add custom criteria, not related to strlen() when the lengths are the same. That's up to you and involves just an additional if
block.
It is important to note that the function must define an order relationship, as in math:
Upvotes: 1
Reputation: 1
Another method:
$f = fn($s1, $s2) => strlen($s1) <=> strlen($s2);
usort($a, $f);
https://php.net/language.operators.comparison
Upvotes: 0
Reputation: 78994
This should do it:
array_multisort(array_map('strlen', $array), $array);
strlen()
using array_map()
array_multisort()
Upvotes: 19
Reputation: 7999
Looks like others have already answered but I started writing this so I'm going to post it, dang it! :)
You could take a look at usort
$data = ["this", "is", "a", "bunch", "of", "words"];
usort($data, function($a, $b) {
$difference = strlen($a) - strlen($b);
return $difference ?: strcmp($a, $b);
});
I'm using the Elvis operator ?:
to just return the difference based on the string lengths if it's not 0. If it is 0, just return the return value of strcmp
Upvotes: 4
Reputation: 730
You can use a custom sort function for this.
function mycmp($a, $b) {
$cmp = strlen($a) - strlen($b);
if ($cmp === 0)
$cmp = strcmp($a, $b);
return $cmp;
}
usort($array, 'mycmp');
Upvotes: 1