arrays - PHP Given increasing numbers in this order, calculate all possible combinations -


the problem simple, giving me headache, have example 4 numbers array, can have 20 numbers. given in following order:

       [1]  [2]  [4]  [5] 

i need obtain combinations of numbers, except numbers can't permuted, order maintained, i'll give example of i'm trying obtain:

     [1] [ ] [4] [5]      [1] [2] [ ] [5]      [ ] [2] [4] [5]      [ ] [ ] [ ] [5]      ,      [ ] [ ] [ ] [ ] 

the resulting array contain sub arrays of resulting numbers

update: empty arrays optional,

          [1] [4]                  or             [2] [4] [5] 

would ok also, less complications.

what want can done quite easily, if realize want accomplish in fact recursive. each subpart need make choice: add space, or add number our list.

function wonkypermutations( $numbers ) {   return wonkypermutationswithspaces( array(), $numbers, count( $numbers ) ); }  function wonkypermutationswithspaces( $prefix, $numbers, $maxlength ) {   if( $maxlength == 0 ) {     //we can't add anymore     return array( $prefix );   } else {     //we have 2 choices: either add space, or don't.     $prefix1 = $prefix;     $prefix1[] = null;     $prefix2 = $prefix;     $prefix2[] = $numbers[0];     $suffix1 = wonkypermutationswithspaces( $prefix1, array_slice( $numbers, 1 ), $maxlength - 1 );     $suffix2 = wonkypermutationswithspaces( $prefix2, array_slice( $numbers, 1 ), $maxlength - 1 );      return array_merge( $suffix1, $suffix2 );   } }  $a = array( 1, 2, 3, 4 ); var_dump( wonkypermutations( $a ) ); 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -