# Array Patterns

Hello,

If you an array that can have 1-24 numbers in it, could be different lenghts.

``````\$set = array(5,6,7,8,15,16,17);
``````

I want to find out how many different sets of number are in the array. like in this array ther are 2 sets of congruent numbers: 5,6,7,8 and 15,16,17.

How can php tell me how many different sets are in an array and what is first and last value of each set is? I looked into preg_match and this whitch does not work:

``````	\$set = array(5,6,7,8,15,16,17);
\$len = count(\$set);

foreach (\$set as \$key => \$value) {
\$new = \$value;
//\$new++;

if(\$value == \$new++){
echo "yes";
} else {
echo "no";
}

echo "New: ". \$new . " Value ". \$value;
}
``````

Thank you,

Boot Straps

Could you make a clear example? Maybe I can help.

oh yes,

I have table of week days and hours. Using Javascript I have an array of the selected hours of a day that are selected. I send the array to php by hidden form field. The array may look like:

``````\$set = array(5,6,7,8,15,16,17);
``````

So in my first post there are 2 sets of congruent sets of numbers: 5,6,7,8 and 15,16,17 each representing in hours on a 24 hours block time. 1st set starts at hour 5 and ends 4 hours later, using 4 how many congruent numbers in a row and so forth for the 2nd set 15,16,17.

``````\$set = array(5,6,7,8,15,16,17);
\$len = count(\$set);
\$kio = array();
foreach (\$set as \$key => \$value) {
\$new = \$key;
\$tuo = \$value - \$key;
\$kio[] = \$tuo;
}

foreach (\$kio as \$key2 => \$value2) {
echo \$key2 . " " . \$value2 . "<br>";
}

\$result = array_unique(\$kio);
print_r(\$result);

\$len2 = count(\$result);
echo  " len2: " . \$len2;

echo "<br><br>there are \$len2 Sets: <br>";
\$i = 0;
\$cua = array_count_values(\$kio);
foreach (\$result as \$key3 => \$value3) {
//echo "<br>" .  \$key3 . " " . \$value3;

foreach (\$cua as \$key4 => \$value4) {
\$value4++;
echo "Set \$i starts with \$value4 and is \$value4 Long <br>";
}
\$i++;
}
``````

Between my first post and now I was able make this. Does not work but very close I think.

So the term set you mean that consists of continue number?

Yes like 123456 but not 1234568.

Hope this help.

``````
\$set = array(5,6,7,8,15,16,17);
\$len = count(\$set);
\$kio = array();

\$previous_number = 0;
\$total_set = 0;
foreach (\$set as \$key => \$value) {
if((\$value-\$previous_number) != 1 && \$key!=0) {
\$total_set++;
}
elseif(\$key==0) \$total_set++;
\$kio[\$total_set][] = \$value;
\$previous_number = \$value;
}

print_r(\$kio);

\$len2 = count(\$kio);
echo  " len2: " . \$len2;

echo "<br><br>there are \$len2 Sets: <br>";
for(\$i=1;\$i<=\$total_set;\$i++)
{
echo "Set \$i starts with ".\$kio[\$i]." and is ".count(\$kio[\$i])." Long <br>";
}

``````

thietkeweb,
You are great. It works prefect. Now I put it in a function and tomorrow i’ll try to get ajax to run the php function.

Thank you thank you.

Boot Straps

You’re welcome. 