Announcement

Collapse
No announcement yet.

Array Question

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Array Question

    Anyone know how to sort an array of strings by the length of the strings?

    I need to find the length of the longest value of an array.

  • #2
    Never mind, I figured it out

    Comment


    • #3
      Originally posted by MUG
      Anyone know how to sort an array of strings by the length of the strings?

      I need to find the length of the longest value of an array.
      i've written this script, works fine for me...
      PHP Code:
      <?php

       
      function cmp($a$b) {
        
      //return strcmp($a, $b); // doesn't work for me
        
      if(strlen($a) == strlen($b))
         return 
      0;
        return (
      strlen($a) < strlen($b)) ? : -1;
       }

       
      $array = array(
        
      => "LOOOOOOOOOOOONGTEST",
        
      => "Test",
        
      => "LongTest",
        
      => "asd",
        
      => "Test12",
        
      => "ksdghjaskgkasgjajgaösjg"
       
      );

       
      usort($array"cmp");

       foreach(
      $array as $val) {
        echo 
      $val."<br />";
       }

      ?>

      Comment

      widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
      Working...
      X