Page 1 of 1

How to find series of values in an array?

Posted: Wed Jun 25, 2008 11:39 pm
by uvaraj24
Hi,

I have following issue, Please can any one help me

Assume Array is:20345,20104,20105,20106,20569,20101,20102,20103,28567,30101,30102,30103,30104,30105

In the above array we need to search whether series of values are appearing, In the above array we have two series values

1. 20101,20102,20103,20104,20105,20106 This need to Display as 20101-->20106

2. 30101,30102,30103,30104,30105 This need to Display as 30101-->30105


At Final Result should Print like this: 20345,20569,28567,20101-->20106,30101-->30105




I have tried like the below

$series=array(20345,20104,20105,20106,20569,20101,20102,20103,28567,30101,30102,30103,30104,30105);

$original=array();
$k=0;
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}

usort($series, "cmp");

foreach ($series as $key => $value) {
$values.=$value.",";
}

$values1=explode(",",$values);
print_r($values1);
echo "<br>";
echo "<br>".$arrlength=count($values1);
echo "<br>".$len=$arrlength-1;
echo "<br>".$len1=$len-1;


for($i=0;$i<$len1;$i++){
for($j=$i+1;$j<$len;$j++){

if(($values1[$i]+1)==$values1[$j]){

$original[$k]=$values1[$i].",";
++$k;

}
}

}
print_r($original);
echo "<br>".$ori_cnt=count($original);
$ori_len1=$ori_cnt-1;
$ori_len2=$ori_len1-1;


$divid=explode(",",$original);
$div_cnt=count($divid);
$dec=$div_cnt-2;
$first=$divid[0]."-".$divid[$dec];

$difference = array_diff($series, $divid);


But i am getting result like 20106,20345,20569,28567,30105,20101-20105,30101-30104

In a particular series last value is not appearing


please help me

Thanks in Advance
Uvaraj