PHP:检查两个数组是否有重复值

时间:2022-06-27 09:08:56

I have two arrays, one that contains a persons first name and another one that contains a persons last name.

我有两个数组,一个包含一个人的名字,另一个包含一个人姓。

$firstname = array ( "John", "Tom", "Ben", "John", "David", "Julie", "David");
$lastname = array ( "Kennedy", "Hyde", "Hughes", "Harper" "Walter", "Weber", "Walter");

A persons first and last names are in both arrays with the same index ( John Kennedy, Tom Hyde, etc.) And I'm searching for a way to find duplicate values in the array $firstname, which would be two Johns and two Davids, but then I'd need to check $lastname with the same indexes if they are duplicate too, so the output would be David Harper, David Harper.

人名和姓在两个阵列中具有相同的索引(约翰肯尼迪,汤姆海德等)我正在寻找一种方法来找到数组$ firstname中的重复值,这将是两个约翰和两个戴维斯,但是如果它们也是重复的话,我需要用相同的索引检查$ lastname,所以输出将是David Harper,David Harper。

The array can contain around 50+ different names and last names.

该数组可以包含大约50多个不同的名称和姓氏。

Couldn't find anything on the internet and I'm running out of ideas, any help would be greatly appreciated.

在互联网上找不到任何东西,我的想法已经用完了,任何帮助都将不胜感激。

2 个解决方案

#1


You have to make a third array that handles the resultant value i.e the full name. The following example will return two arrays, duplicates array and fullnames array without duplicates:

您必须创建一个处理结果值的第三个数组,即全名。以下示例将返回两个数组,duplicates数组和fullnames数组,没有重复:

    <?php

$firstname = array ( "John", "Tom", "Ben", "John", "David", "Julie", "David");
$lastname = array ("Kennedy", "Hyde", "Hughes", "Harper", "Walter", "Weber", "Walter");
$fullname = array();
$duplicates = array();

// foreach time we have a first name.
for ($i =0; $i < count($firstname); $i++){
    $fullname_tmp = $firstname[$i]." ".$lastname[$i];
    if (in_array($fullname_tmp, $fullname)){
        if (in_array($fullname_tmp, $duplicates)){
            $duplicates[] = $fullname_tmp;
        }
        else{
            $duplicates[] = $fullname_tmp;
            $duplicates[] = $fullname_tmp;
        }

    }
    else{
        $fullname[] = $fullname_tmp;
    }
}

echo "<pre>\n duplicates \n";

print_r($duplicates);

echo "\n full names \n";

print_r($fullname);

The example is using in_array and a simple for loop.

该示例使用in_array和一个简单的for循环。

Checkout this DEMO

看看这个DEMO

#2


Wouldn`t simple array_unique solve your problem?

不会简单的array_unique解决你的问题吗?

$firstname = array ( "John", "Tom", "Ben", "John", "David", "Julie", "David");
$lastname = array ( "Kennedy", "Hyde", "Hughes", "Harper", "Walter", "Weber", "Walter");
var_dump(array_unique($firstname));
var_dump(array_unique($lastname));

And the output will be

输出将是

array (size=5)
  0 => string 'John' (length=4)
  1 => string 'Tom' (length=3)
  2 => string 'Ben' (length=3)
  4 => string 'David' (length=5)
  5 => string 'Julie' (length=5)
array (size=6)
  0 => string 'Kennedy' (length=7)
  1 => string 'Hyde' (length=4)
  2 => string 'Hughes' (length=6)
  3 => string 'Harper' (length=6)
  4 => string 'Walter' (length=6)
  5 => string 'Weber' (length=5)

#1


You have to make a third array that handles the resultant value i.e the full name. The following example will return two arrays, duplicates array and fullnames array without duplicates:

您必须创建一个处理结果值的第三个数组,即全名。以下示例将返回两个数组,duplicates数组和fullnames数组,没有重复:

    <?php

$firstname = array ( "John", "Tom", "Ben", "John", "David", "Julie", "David");
$lastname = array ("Kennedy", "Hyde", "Hughes", "Harper", "Walter", "Weber", "Walter");
$fullname = array();
$duplicates = array();

// foreach time we have a first name.
for ($i =0; $i < count($firstname); $i++){
    $fullname_tmp = $firstname[$i]." ".$lastname[$i];
    if (in_array($fullname_tmp, $fullname)){
        if (in_array($fullname_tmp, $duplicates)){
            $duplicates[] = $fullname_tmp;
        }
        else{
            $duplicates[] = $fullname_tmp;
            $duplicates[] = $fullname_tmp;
        }

    }
    else{
        $fullname[] = $fullname_tmp;
    }
}

echo "<pre>\n duplicates \n";

print_r($duplicates);

echo "\n full names \n";

print_r($fullname);

The example is using in_array and a simple for loop.

该示例使用in_array和一个简单的for循环。

Checkout this DEMO

看看这个DEMO

#2


Wouldn`t simple array_unique solve your problem?

不会简单的array_unique解决你的问题吗?

$firstname = array ( "John", "Tom", "Ben", "John", "David", "Julie", "David");
$lastname = array ( "Kennedy", "Hyde", "Hughes", "Harper", "Walter", "Weber", "Walter");
var_dump(array_unique($firstname));
var_dump(array_unique($lastname));

And the output will be

输出将是

array (size=5)
  0 => string 'John' (length=4)
  1 => string 'Tom' (length=3)
  2 => string 'Ben' (length=3)
  4 => string 'David' (length=5)
  5 => string 'Julie' (length=5)
array (size=6)
  0 => string 'Kennedy' (length=7)
  1 => string 'Hyde' (length=4)
  2 => string 'Hughes' (length=6)
  3 => string 'Harper' (length=6)
  4 => string 'Walter' (length=6)
  5 => string 'Weber' (length=5)