Jump to content

php function to group/sort numbers


luvz2drv

Recommended Posts

this is what I have

an array of numbers  1,2,3,4,5,11,17,18,19,24

 

what I need to do is have it print 1-5,11,17-19,24

 

can any one please help me get this snipet of code constructed

 

thanks

 

Jonathon

 

is the only thing I found close but it is not for php... 

use Set::IntSpan; 
use strict;
sub consecutive_integers_list {
    my(@integers) =sort { $a <=> $b } @{+shift};
    my $set = Set::IntSpan->new(join ",", @integers);
    $set->run_list;   
}             
my @integers = qw( 20 2 3 4 5 6 7 23 19 17 25 30 11 12 22 21 68 103829
+ 24 18 );
print consecutive_integers_list(\@integers);

//prints: 2-7,11-12,17-25,30,68,103829

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.