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"]]
Write code something like this :
$first = array( 2, 3, 5, 7);
$second = array(2, 3, 5, 23);
$prime = "Prime Factors of 420:";
$scndprim = "Prime Factors of 690:";
foreach ($first as $key => $value) {
if(in_array($value, $second))
{
$prime .="<b>".$value."</b>,";
$array_common[] = $value;
}else{
$prime .=$value.",";
$array_uncommon[] = $value;
}
// ========= for second array
if(in_array($second[$key], $first))
{
$scndprim .="<b>".$second[$key]."</b>,";
}else{
$scndprim .=$second[$key].",";
$array_uncommon[] = $second[$key];
}
}
echo "<pre />";
print_r(trim($prime,','));
print_r(trim($scndprim,','));
//-- Common and uncommon arrays
echo " <br>common elements are ";
print_r(join(",",$array_common));
echo " <br>uncommon elements are ";
print_r(join(",",$array_uncommon));
I found a way, but i think it is not the most efficient way, there will be more efficient answers.
<?php
$factor_list_1_old = array(2, 2, 3, 5, 7);
$factor_list_2_old = array(2, 3, 5, 23);
$commons = array();
$uncommons = array();
foreach($factor_list_1_old as $key => $value){
if(in_array($value, $factor_list_2_old)){
array_push($commons, $value);
}else{
array_push($uncommons, $value);
}
}
foreach($factor_list_2_old as $key => $value){
if(in_array($value, $factor_list_1_old)){
array_push($commons, $value);
}else{
array_push($uncommons, $value);
}
}
var_dump(array_unique($uncommons));
var_dump(array_unique($commons));
?>
$factor_list_1_old = array(2 , 2 , 3 , 5 , 7);
$factor_list_2_old = array(2 , 2, 3 , 5 , 13);
foreach ($factor_list_1_old as $key => $value) {
if(in_array($value, $factor_list_2_old))
{
$firstList .= "<b>".$value."</b>,";
$key2 = array_search($value, $factor_list_2_old);
$commonArray[] = $value;
}else{
$firstList .= "<span style='color:red'>".$value."</span>,";
$uncommonArray1[] = $value;
}
print_r($factor_list_1_old);
if(in_array($factor_list_2_old[$key], $factor_list_1_old))
{
$secondList .="<b>".$factor_list_2_old[$key]."</b>,";
//unset($factor_list_2_old[$key]);
}else{
$secondList .= "<span style='color:red'>".$factor_list_2_old[$key]."</span>,";
$uncommonArray2[] = $factor_list_2_old[$key];
}
unset($factor_list_1_old[$key]);
unset($factor_list_2_old[$key2]);
}
echo "Prime Factors of 420 are ".$firstList."<br />";
echo "Prime Factors of 780 are ".$secondList."<br />";
echo "Common Factors of 420 and 780 are ".implode(',',$commonArray)."<br />";
echo "Un-Common Factors of 420 are ".implode(',',$uncommonArray1)."<br />";
echo "Un-Common Factors of 780 are ".implode(',',$uncommonArray2);
If
$factor_list_1_old = array(2 , 2 , 3 , 5 , 7);
$factor_list_2_old = array(2 , 2, 3 , 5 , 13);
output
If
$factor_list_1_old = array(2 , 2 , 3 , 5 , 7);
$factor_list_2_old = array(2 , 3 , 5 , 13);
output
$prime_420 = array(
2,
3,
5,
7
);
$prime_690 = array(
2,
3,
5,
23
);
$d1 = array_diff($prime_420, $prime_690);
$d2 = array_diff($prime_690, $prime_420);
$uncommon = array_merge($d1, $d2);
$common = array_intersect($prime_420, $prime_690);
echo "Primes of 420 : ";
for ($i = 0; $i < count($prime_420); $i++) {
if (in_array($prime_420[$i], $uncommon)) {
echo "<font color='#ff0000'>" . $prime_420[$i] . "</font>,";
} else {
echo $prime_420[$i] . ",";
}
}
echo "<br />";
echo "Primes of 690 : ";
for ($i = 0; $i < count($prime_690); $i++) {
if (in_array($prime_690[$i], $uncommon)) {
echo "<font color='#ff0000'>" . $prime_690[$i] . "</font>,";
} else {
echo $prime_690[$i] . ",";
}
}