help with converting javascript function to php
Posted
by Haroldo
on Stack Overflow
See other posts from Stack Overflow
or by Haroldo
Published on 2010-04-19T15:31:46Z
Indexed on
2010/04/19
15:43 UTC
Read the original article
Hit count: 211
php
|JavaScript
My javascript isnt so great, but i found a brilliant looking function here
I'm not sure what to do with this bit:
var ranges = [], rstart, rend;
full function:
function getRanges(array) {
var ranges = [], rstart, rend;
for (var i = 0; i < array.length; i++) {
rstart = array[i];
rend = rstart;
while (array[i + 1] - array[i] == 1) {
rend = array[i + 1]; // increment the index if the numbers sequential
i++;
}
ranges.push(rstart == rend ? rstart+'' : rstart + '-' + rend);
}
return ranges;
}
getRanges([2,3,4,5,10,18,19,20]);
// returns ["2-5", "10", "18-20"]
getRanges([1,2,3,5,7,9,10,11,12,14 ]);
// returns ["1-3", "5", "7", "9-12", "14"]
getRanges([1,2,3,4,5,6,7,8,9,10])
// returns ["1-10"]
© Stack Overflow or respective owner