I am working on a genetics calculator which sort of works, however not as I intend it to haha.
I am using the following code:
<?php
function getGeneArray($geneString) {
// check the argument for fitting your needs!
if ( strlen($geneString) % 2 == 1 ) {
die('Supplied geneString is not made of pairs!'); // better not die - handle errors according to your application methodology
}
// add additional error-catching (there are only certain possible base-pairs, if something else is found you should reject the string
$genes = array();
$i = 0;
while ( $i < strlen($geneString) )
{
$genes[] = substr($geneString, $i, 2);
$i += 2; // Here is your mistake, you just $i++
}
return $genes;
}
function combinePairs($A, $B) {
// uppercase genes build the string first, which means B must be uppercase to come first and A cant be uppercase -- no longer important
if ($A !== $A && $B === $B) {
return $B.$A;
}
return $A.$B;
}
$gene1 = 'bbEe';
$gene2 = 'bbEe';
$geneA = getGeneArray($gene1);
$geneB = getGeneArray($gene2);
$possibilities = array();
foreach ($geneA as $A) {
foreach ($geneB as $B) {
$possibilities[] = combinePairs($A, $B);
}
}
The output from running the above code is:
Array ( [0] => bbbb [1] => bbEe [2] => Eebb [3] => EeEe )
When in reality I would like it to be something like:
Array ( [0] => bbee [1] => bbEe [2] => bbEE )
What can I do/change to get the right sort of outcome?