Let’s say that I have the following sequence of numbers in an array:
$numbers = array(1,3,2,23,24,25,26, 8)
How can I print them in ranges, for instance:
The numbers are 1-3, 23-26, 8.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Here’s a simple version, creating
groupscontaining your rangesThe output is
Now, if the order of your ranges is important, like you described in your question, you can still sort your groups… from what I can see, you want the ranges first followed by the single values? This can be done by adding
right before the
foreachand the output becomes: