-1

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.

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

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

Tejus Prasad
  • 6,322
  • 7
  • 47
  • 75
  • What do you want to do if both names are the same? – Rizier123 May 10 '15 at 09:35
  • Nothing, they need to be skipped. – Deividas Vysniauskas May 10 '15 at 09:39
  • So would it also work if you just remove them? – Rizier123 May 10 '15 at 09:42
  • 1
    check this..http://stackoverflow.com/questions/1170807/how-to-detect-duplicate-values-in-php-array – Coder anonymous May 10 '15 at 09:46
  • So, is it really important to find duplicate first names? Sounds like you just want to do something if first name + last name results in a duplicate...!? In that case, just combine first and last names together into one name and filter that by duplicates. – deceze May 10 '15 at 09:51
  • _“I have two arrays, one that contains a persons first name and another one that contains a persons last name”_ – that is quite a bad data structure to begin with. You should rather have _one_ array of “people”, that consists of arrays (or maybe objects) holding the first and last name for one person each. – CBroe May 10 '15 at 10:24
  • 1
    I think several people have missed the point of the question! Doesn't deserve downvotes in my opinion. – Drumbeg May 10 '15 at 11:37

2 Answers2

3

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:

    <?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.

Checkout this DEMO

TheCrazyProfessor
  • 919
  • 1
  • 15
  • 31
SaidbakR
  • 13,303
  • 20
  • 101
  • 195
0

Wouldn`t simple array_unique solve your problem?

$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)
  • I think the idea is to eliminate elements that have a common forename AND surname sharing the same array index. – Drumbeg May 10 '15 at 09:58
  • I don't need to remove the duplicates, I need to output the duplicates. All of the names and last names that don't share a duplicate value can be removed. – Deividas Vysniauskas May 10 '15 at 09:59