search

Home  >  Q&A  >  body text

How to display continuous values ​​in groups? Please give me guidance and help from teachers.

It’s not a database operation, it’s a PHP operation.

For example:

$a="1,2,3,4,5,6,7,8,9,10,11"

$b=" 8,9,10,11,12,13,17,18,19,20"

$c="10,11,12,13,14,15,16,18,20,21, 22,23,24"

Required results

$a="1-11"

$b="8-13,17 -20"

$c="10-16,18,20-24"

Please give me guidance

JD26JD261710 days ago741

reply all(1)I'll reply

  • 一锤一座山

    一锤一座山2020-04-22 11:15:34

    Let’s be a bit stupid and split it first $strList = explode(",",$str);

    After that, we can loop from the first or last one and compare 1 and -1.

    When not equal, output the start bit and end bit, and then continue to the next step of the cycle.

    You can also use recursion.

    reply
    0
  • Cancelreply