匹配数组值对(PHP)

时间:2013-11-20 05:07:16

标签: php arrays

我和HOURS一直在为此而斗争,我完全陷入困境。

我随机生成数字并找到他们的主要因素。例如......

素数因子420:2,2,3,5,7 主要因素为690:2,3,5,23

我想在列出它们时分别突出显示匹配对和“不常见”因素。所以,在这种情况下,我想要一些像......

素数因子420: 2 ,2, 3 5 ,7
主要因素为690: 2 3 5 ,23

然后420中的另外2和7以及690中的23将以红色突出显示(例如)。

我已经在数组中分别列出了素因子($factor_list_1_old$factor_list_2_old)。我还列出了数组中的公共因子列表($commons)以及数组中不常见因子的列表($uncommons)。

我尝试了很多方法,似乎没有任何方法适用于所有场景。我可以让这个场景起作用,但是对于像420和780这样的东西它会失败。

有什么想法吗?

5 个答案:

答案 0 :(得分:1)

我的功能

$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"]]

答案 1 :(得分:0)

编写类似这样的代码:

   $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)); 

答案 2 :(得分:0)

我找到了一种方法,但我认为这不是最有效的方式,会有更有效的答案。

<?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));
?>

答案 3 :(得分:0)

$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);

如果

$factor_list_1_old = array(2 , 2 , 3 , 5 , 7);
$factor_list_2_old = array(2 , 2,  3 ,  5 ,  13);

输出

enter image description here

如果

$factor_list_1_old = array(2 , 2 , 3 , 5 , 7);
$factor_list_2_old = array(2 ,  3 ,  5 ,  13);

输出

enter image description here

答案 4 :(得分:0)

$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] . ",";
    }
}