7 number combiantions from an array - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

7 number combiantions from an array

nrkamleshnrkamlesh Posts: 6Member
Please help me with this

:
:
: n = 20
: r = 5
:
: var combination = new Array(r);
:
: for(i=0; i r)
: index = r;
: while(combination[index-1]==n+index-r && index>0)
: index--;
: if(index==0)
: return false;
: combination[index-1]++;
: for(index++; index<=r; index++)
: combination[index-1] = combination[index-2]+1;
: }
: return true;
: }
: function showNext() {
: val = getNextCombination(n, r);
: if(!val) // No more combinations found
: return val;
:
: txt = "";
: for(i in combination)
: txt+=combination[i]+", ";
:
: // Show the combination somewhere
: window.status = txt;
:
:
: return val;
: }
: function init() {
: for(p=0; p<50000 && showNext(); p++); // Shows the first 50000 combinations or as many as there are
: }
: </script>
:
:
:
:


i want the same result in in php and my condition is i want to pass different king of numbers in . now this combinations goes sequence number like 1,2,3,4,5,6 and i want a combinations of different numbers from a array like i need a result of array(1,12,32,24,14) and r value is default 7 . can any one provide me a solution for this in php.

Thank in advance who ever helps me .

Comments

  • CJNimesCJNimes Posts: 13Member
    Well, replace "" for "<?php", "</script>" for "?>", delete the "var" word and add a "$" sign on each variable and you will get your php code ;)
  • nrkamleshnrkamlesh Posts: 6Member
    i did this before and some of the functions are not running like for(i in var).
  • CJNimesCJNimes Posts: 13Member
    I missed a few more changes you have to do:

    $combination = array();
    ...
    ...

    function getNextCombination
    global $combination; <------------ add this as function first line
    ...
    ...


    function showNext
    global $combination, $n, $r; <-------- add this line
    ...
    ...


    Replace this:
    for(i in combination)
    txt+=combination[i]+", ";
    window.status = txt;

    With this:
    foreach ($combination as $c)
    $txt.=$c.", ";
    echo $txt."<br>";

    Delete all html code, and the last line shoud be:
    init();


    I thinks now you can get it!
  • nrkamleshnrkamlesh Posts: 6Member
    Thank you very much CJNimes. I got the result Hurrey..
Sign In or Register to comment.