Re: Sort by string length...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 ($x < $y) ? -1 : 1;
}
$array = array('Longest string','short','longer');
usort($array,'cmp');
?>

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux