PHP- How to reduce for-loop speed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kamlesh
Forum Newbie
Posts: 1
Joined: Mon Jul 21, 2008 6:10 am

PHP- How to reduce for-loop speed

Post by kamlesh »

Code: Select all

<?php
// I need the combinations for this commented numbers   
// 1, 7, 13, 19, 25, 31, 2, 8, 14, 20, 26, 32, 3, 9, 15, 
// 21, 27, 33, 4, 10, 16, 22, 28, 34, 5, 11, 17, 23, 29,
// 35, 6, 12, 18, 24, 30, 36
$string=array(1,7,13,19,25,31,2,8,14,20,26,32,3,9,15,21,27,33,4,10,16,22,28,34);
$len=count($string);
$c=0;
ob_start();
for ($e = 0; $e < $len - 6; $e++)
{
   for ($f = $e+1; $f < $len - 5; $f++)
   {
       for ($g = $f+1; $g < $len - 4; $g++)
       {
           for ($h = $g+1; $h < $len - 3; $h++)
           { 
               for ($i = $h+1; $i < $len - 2; $i++)
               {
                   for ($j = $i + 1; $j < $len - 1; $j++)
                   {
                        for ($k = $j + 1; $k < $len; $k++)
                        {
                             $c++;
                             $output[] = $string[$e] . "," . 
                                         $string[$f] . "," . 
                                         $string[$g] . "," .  
                                         $string[$h] . "," . 
                                         $string[$i] . "," . 
                                         $string[$j] . "," . 
                                         $string[$k];
                             ob_flush();
                        }
                        ob_flush();
                   }
                   ob_flush();
               }
               ob_flush();
           }
           ob_flush();
   }
   ob_flush();
}
ob_flush();
}
echo count($output);
?>
And I need the output same like i mentioned below. Output: passed numbers $string=array(1, 7, 13, 19, 25, 31, 2, 8, 14) and the out put is below count of combinations = 36 Array ( [0] => 1,7,13,19,25,31,2 [1] => 1,7,13,19,25,31,8 [2] => 1,7,13,19,25,31,14 [3] => 1,7,13,19,25,2,8 [4] => 1,7,13,19,25,2,14 [5] => 1,7,13,19,25,8,14 [6] => 1,7,13,19,31,2,8 [7] => 1,7,13,19,31,2,14 [8] => 1,7,13,19,31,8,14 [9] => 1,7,13,19,2,8,14 [10] => 1,7,13,25,31,2,8 [11] => 1,7,13,25,31,2,14 [12] => 1,7,13,25,31,8,14 [13] => 1,7,13,25,2,8,14 [14] => 1,7,13,31,2,8,14 [15] => 1,7,19,25,31,2,8 [16] => 1,7,19,25,31,2,14 [17] => 1,7,19,25,31,8,14 [18] => 1,7,19,25,2,8,14 [19] => 1,7,19,31,2,8,14 [20] => 1,7,25,31,2,8,14 [21] => 1,13,19,25,31,2,8 [22] => 1,13,19,25,31,2,14 [23] => 1,13,19,25,31,8,14 [24] => 1,13,19,25,2,8,14 [25] => 1,13,19,31,2,8,14 [26] => 1,13,25,31,2,8,14 [27] => 1,19,25,31,2,8,14 [28] => 7,13,19,25,31,2,8 [29] => 7,13,19,25,31,2,14 [30] => 7,13,19,25,31,8,14 [31] => 7,13,19,25,2,8,14 [32] => 7,13,19,31,2,8,14 [33] => 7,13,25,31,2,8,14 [34] => 7,19,25,31,2,8,14 [35] => 13,19,25,31,2,8,14 )

The 36 numbers are not predefined. so it may vary depends upon the user selection. some might select 10 numbers like for example (1,10,14,32,12,18,20,5,12,8) and some might above 10. But maximum numbers are 36. So the combinations will be different each time and i am not going to store these combinations and I just want to generate combinations (not to print in a web page) . I am going to match the result(7 numbers) with these combinations and then i will take a count of matching combinations with the result 7 numbers and store in the database. I need same output in short execution period. Thanks in advance
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP- How to reduce for-loop speed

Post by Jonah Bron »

A quick google found a solution here.

Cheers.
Post Reply