开发者

Compare dynamic arrays

I would like help finishing my function that compares two arrays and returns the values of the element that are the same in both.

$array1 = array("bob","mike","david","gary");
$array2 = array("susan","jenny","mike");

The two arrays will hav开发者_如何学编程e different amounts of elements every time. I run the function below and it says there are matches but wont tell me which ones. Also will my function work if the array does not have the same amount of elements?

echo  find_matches($array1,$array2);


function find_matches($mac1, $mac2){

   $matches = array();

   foreach($mac1 as $mac){
         if(in_array($mac, $mac2)){
            $matches[] = $mac;
         }
   }

    if ($matches != 0){

    $error_message = "The following numbers match: " . implode(' ', $matches);

    return $error_message;
    }else{
    return true;
    }
}


If you're looking to return the number of matches and the matches themselves, you could use array_intersect:

$matches = array_intersect($array1, $array2);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜