Grouping arrays with consecutive keys

Posted by KPL on Stack Overflow See other posts from Stack Overflow or by KPL
Published on 2011-01-05T05:51:30Z Indexed on 2011/01/05 5:53 UTC
Read the original article Hit count: 203

Filed under:

Hello people,

I've an array like this -

 Array
(
    [16] => 424
    [17] => 404
    [18] => 416
    [21] => 404
    [22] => 456
    [23] => 879
    [28] => 456
    [29] => 456
    [32] => 123
    [35] => 465
)

The output of this array would be

Array
(
    [0] => Array
           (   ['start'] => 16
               ['stop'] => 18
           )
    [1] => Array
           (   ['start'] => 21
               ['stop'] => 23
           )
    [2] => Array
           (
               ['start'] => 28
               ['stop'] => 29
           )
    [3] => Array
           (
               ['start'] => 32
               ['stop'] => 33
           )
    [4] => Array
           (
               ['start'] => 35
               ['stop'] => 36
           )
)

I don't really need the values. Just grouping the keys.

And if consecutive integer doesn't exist for a particular key(like for [32] and [35]), 'stop' should be the consecutive integer.

Thank you all for help.

© Stack Overflow or respective owner

Related posts about php