Prev: Caution Fraud Alert about Antony Papamichail
Next: Removing link on the fly, but leave link text
From: Richard Quadling on 31 Aug 2010 12:49 On 31 August 2010 17:39, Richard Quadling <rquadling(a)gmail.com> wrote: > On 31 August 2010 17:06, Tontonq Tontonq <rootdot(a)gmail.com> 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
From: Richard Quadling on 31 Aug 2010 12:52 On 31 August 2010 17:49, Richard Quadling <rquadling(a)gmail.com> wrote: > On 31 August 2010 17:39, Richard Quadling <rquadling(a)gmail.com> wrote: >> On 31 August 2010 17:06, Tontonq Tontonq <rootdot(a)gmail.com> wrote: >>> Array >>> ( >>> [300] => 300 >>> [301] => 301 >>> ... If you add a ... sort($data) ... just before the foreach() loop... Array ( [0] => 101 [1] => 165-173 [2] => 201-215 [3] => 300-316 [4] => 987 ) Watch out for duplicate values. I changed 315 and 316 to 305 and 306... unsorted ... Array ( [0] => 300-314 [1] => 165-173 [2] => 201-215 [3] => 101 [4] => 305-306 [5] => 987 ) sorted ... Array ( [0] => 101 [1] => 165-173 [2] => 201-215 [3] => 300-305 [4] => 305-306 [5] => 306-314 [6] => 987 ) -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
First
|
Prev
|
Pages: 1 2 3 4 Prev: Caution Fraud Alert about Antony Papamichail Next: Removing link on the fly, but leave link text |