Any idea how to sort an array by string length?
Russ Jones
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Russell P Jones wrote:
Any idea how to sort an array by string length?
Use usort() in conjunction with a user defined function that compares the
length of
both strings using strlen(). If brevity at the (possible) expense of clarity is
your thing, you can even use create_function() as your
Hi,
Wednesday, December 22, 2004, 7:18:52 AM, you wrote:
RPJ Any idea how to sort an array by string length?
RPJ Russ Jones
With a user defined sorting function something like this
?php
function cmp($a, $b){
$x = strlen($a);
$y = strlen($b);
if ($x == $y) {
return 0;
}
return
On Wednesday 22 December 2004 05:18, Russell P Jones wrote:
Any idea how to sort an array by string length?
usort(), strlen()
--
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design Hosting * Internet Intranet Applications Development *
One of the Array sort functions?
http://www.php.net/usort
Russell P Jones wrote:
Any idea how to sort an array by string length?
Russ Jones
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
How about iterating though the strings and putting string values and
lengths in an array:
[begin untested code]
$strings = array('i','am','who');
$string_count = array();
foreach ($strings as $string) {
$string_count['value'][] = $string;
$string_count['length'][] = strlen($string);
}
Is it an associative array? That is, are you assigning values like this:
Method 1:
$arr[] = some text string;
or
$arr = array(some text string,some text string too);
or like this...
Method 2:
$arr[value1] = some text string;
or
$arr1 = array(value1=some text string,value2=some text string
On Tue, 21 Dec 2004 16:18:52 -0500 (EST), you wrote:
Any idea how to sort an array by string length?
You write a function that compares two strings (A and B), and returns
0 if len(A) == len(B), -1 if len(A) len(B) or +1 if len(A) len(B).
function compare_by_length ($a, $b)
{
$la = strlen
8 matches
Mail list logo