Reputation: 77
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 ";"
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
109 ; + ; - ; + ; ND ; +
109 ; + ; - ; - ; ND ; +
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,
Upvotes: 1
Views: 222
Reputation: 12837
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);
}
Upvotes: 2