On 31 August 2010 17:39, Richard Quadling <[email protected]> wrote:
> On 31 August 2010 17:06, Tontonq Tontonq <[email protected]> wrote:
>> Array
>> (
>> [300] => 300
>> [301] => 301
>> ...
Not sure what happened there!
<?php
$data = array
(
300 => 300,
301 => 301,
302 => 302,
303 => 303,
304 => 304,
305 => 305,
306 => 306,
307 => 307,
308 => 308,
309 => 309,
310 => 310,
311 => 311,
312 => 312,
313 => 313,
314 => 314,
165 => 165,
166 => 166,
167 => 167,
168 => 168,
169 => 169,
170 => 170,
171 => 171,
172 => 172,
173 => 173,
201 => 201,
202 => 202,
203 => 203,
204 => 204,
205 => 205,
206 => 206,
207 => 207,
208 => 208,
209 => 209,
210 => 210,
211 => 211,
212 => 212,
213 => 213,
214 => 214,
215 => 215,
101 => 101,
315 => 315,
316 => 316,
987 => 987,
);
$newData = array();
$firstValue = null;
$lastValue = null;
foreach($data as $value)
{
// New first value.
if (is_null($firstValue))
{
$firstValue = $value;
$lastValue = null;
}
// New last value and is the same or 1 more or ongoing value
else if ((is_null($lastValue) && $value == 1 + $firstValue) || $value
== 1 + $lastValue)
{
$lastValue = $value;
}
// We have a break;
else
{
if (!is_null($lastValue))
{
$newData[] = "$firstValue-$lastValue";
}
else
{
$newData[] = "$firstValue";
}
$lastValue = null;
$firstValue = $value;
}
}
if (!is_null($firstValue))
{
if (!is_null($lastValue))
{
$newData[] = "$firstValue-$lastValue";
}
else
{
$newData[] = "$firstValue";
}
}
print_r($newData);
?>
outputs ...
Array
(
[0] => 300-314
[1] => 165-173
[2] => 201-215
[3] => 101
[4] => 315-316
[5] => 987
)
--
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php