I've been battling with this for HOURS and I am completely stuck.
I'm randomly generating numbers and finding their prime factors. For example...
Prime Factors of 420: 2, 2, 3, 5, 7
Prime Factors of 690: 2, 3, 5, 23
I want to highlight the matching pairs and "uncommon" factors separately as I list them out. So, in this case I would want something like...
Prime Factors of 420: 2, 2, 3, 5, 7
Prime Factors of 690: 2, 3, 5, 23
Then the other 2 and the 7 from 420, and the 23 from 690 would be highlighted in red (for example).
I already have the lists of prime factors in arrays ($factor_list_1_old and $factor_list_2_old respectively). I also have the list of common factors in an array ($commons) and a list of the uncommon factors in an array ($uncommons).
I've tried many ways of doing this and nothing seems to work for all scenarios. I can get this scenario to work, but then it fails for something like 420 and 780.
Any ideas?
My function
$array1 = array(2, 2, 3, 5, 7);
$array2 = array(2, 3, 5, 23);
function highlightFactors($factors, $other_factors)
{
$result = array();
foreach ($factors as $factor)
{
if (($found_key = array_search($factor, $other_factors)) === false)
{
$result[] = array($factor, 'normal');
}
else
{
$result[] = array($factor, 'bold');
unset($other_factors[$found_key]);
}
}
return $result;
}
echo json_encode(highlightFactors($array1, $array2));
// [[2,"bold"],[2,"normal"],[3,"bold"],[5,"bold"],[7,"normal"]]
echo json_encode(highlightFactors($array2, $array1));
// [[2,"bold"],[3,"bold"],[5,"bold"],[23,"normal"]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With