1

i need to discover all possible combinations for some arrays that have an specific char.

let me explain.

i have this text that have vectors organized as rows:

1342;-;+;V;+;-;V;V;V;ND;-;-;ND;ND;+;+;ND;-;ND;ND;ND;-;+;ND;+;-;-;-;ND;-;-;ND;ND;-;ND;ND;F;ND
2343;-;-;-;-;-;-;ND;-;ND;-;-;ND;ND;-;V;ND;V;ND;ND;ND;-;+;ND;-;-;-;V;ND;V;-;ND;ND;-;ND;ND;O;ND
2344;-;-;-;-;-;-;ND;-;ND;-;ND;ND;ND;ND;+;ND;+;ND;ND;ND;-;+;ND;+;-;-;-;ND;V;ND;ND;ND;-;ND;ND;O;ND
2345;-;V;V;+;V;V;ND;-;+;-;-;-;ND;-;V;ND;+;ND;V;ND;-;+;ND;-;-;-;-;V;-;+;ND;ND;-;-;+;F;-

each row is an vector..the attributes values are separeted by ";"

  • all possible values are: numbers, +, -, O, F, ND and 'V'
  • but the char V means + and -

what i need to to is to check for EACH ROW when the vector has in any attibute the char "V" because if have it means that this row must be replicated to all possibilities of V.

see this little example:

109 ; + ; - ; V ; ND ; +

i checked that this row has one or more than one "V" so now will calculate all possibilities that can be generated for this row

as we know V = + and - so this one row will generate:

109 ; + ; - ;  +  ; ND ; +
109 ; + ; - ;  -  ; ND ; +

as we have just one V so we will have 2 possibilities... the problem is when there is more than one 'V' .. how to calculate all combinations to it..

when creating the new text with all rows combinations the new text must not contain the origin row with the 'V'. note that all other attributes values remains the same.

thank you,

1 Answer 1

2

You can do it recursively. This solution assumes a limited dataset, where we wouldn't run the risk of exhausting memory.

<?php

$input = "5;V\n4;3";

$input_arr = explode("\n", $input);
foreach( $input_arr as $vector ){
    $output = replace_v(explode(';', $vector));
    foreach( $output as $output_line ){
        echo implode(';', $output_line) . "\n";
    }
}

function replace_v($input) {
    if( $key = array_search('V', $input) ){
        $a = $b = $input;
        $a[$key] = '-';
        $b[$key] = '+';
        return array_merge(replace_v($a), replace_v($b));
    }
    return array($input);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hello Greg thank you, but i think there is some mistakes: Notice: Undefined variable: vector in C:\wamp\www\vector_v_combination\index.php on line 19 and Warning: array_search() expects parameter 2 to be array, null given in C:\wamp\www\vector_v_combination\index.php on line 19
maybe because in the " function replace_v($input) { if( $key = array_search('V', $vector) ){ " .. $vector should be $input that's it ?
even doing this it returns me the wrong answer: 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; + 109 ; + ; - ;-; ND ; + ;109 ; + ; - ;+; ND ; +
Greg , thank you so much ! that's a great fucntion! good work ! thanks !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.