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
https://forums.phpfreaks.com/topic/70608-php-function-to-groupsort-numbers/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.