Jump to content

removing duplicate entries in a 2 dimensional array


Yure

Recommended Posts

I dont even know where to begin doing this. I am supposed to remove duplicate entries in this 2 dimensional array but I just can't find any answers anywhere.

 

<?php
/*
In the following two dimensional array (an array of arrays) you will notice that
there are several duplicate entries (David and Patricia).

1) Write a basic function which will go through $input and remove the duplicate
	entries. A "duplicate" is an entry which has the same FirstName and LastName
	as another entry. Ignore the other fields. Call your function "dedupe1". It
	should return a two-dimensional array without these duplicates

2) Using your dedupe1 function, write one called dedupe2 which will detect empty
	DOB's and DOB's of '00/00/0000'. When an empty or zeroed DOB is detected, your
	code should use the entry with a complete DOB (if available). Like dedupe1
	your code should return a two-dimensional array without duplicates. If you
	use the provided $input function, all of the returned entries should have a
	complete DOB listed.

*/

$input = array(
array(
	'FirstName' => 'Daniel',
	'LastName' => 'Anderson',
	'Phone' => '614-123-4568',
	'Address' => '123 Main St',
	'SSN' => '001-01-0001',
	'DOB' => '01/11/1922'
),
array(
	'FirstName' => 'Aaron',
	'LastName' => 'Williams',
	'Phone' => '937-321-3993',
	'Address' => '933 N Park St',
	'SSN' => '992-23-1192',
	'DOB' => '04/21/1965'
),
array(
	'FirstName' => 'David',
	'LastName' => 'Taylor',
	'Phone' => '223-293-9921',
	'Address' => '123 Main St',
	'SSN' => '003-19-2992',
	'DOB' => '12/14/1995'
),
array(
	'FirstName' => 'Patricia',
	'LastName' => 'Anderson',
	'Phone' => '614-123-4568',
	'Address' => '123 Main St',
	'SSN' => '123-32-3123',
	'DOB' => '00/00/0000'
),
array(
	'FirstName' => 'David',
	'LastName' => 'Taylor',
	'Phone' => '223-293-9921',
	'Address' => '123 Main St',
	'SSN' => '003-19-2992',
	'DOB' => ''
),
array(
	'FirstName' => 'Patricia',
	'LastName' => 'Anderson',
	'Phone' => '614-123-4568',
	'Address' => '123 Main St',
	'SSN' => '123-32-3123',
	'DOB' => '02/22/1957'
),
);

// Get results from dedupe1 and print them
$result1 = dedupe1($input);
var_dump($result1);

// Get results from dedupe2 and print them
$result2 = dedupe2($input);
var_dump($result2);

print_r($input);//just to see the information in the web page

function dedupe1($in_array) {
// your code here
}

function dedupe2($in_array) {
// your code here
}

?>

Link to comment
Share on other sites

Have you looked at the manual and the functions available for arrays? http://www.php.net/manual/en/ref.array.php

 

There are a lot of array functions and it can be daunting, but if you took 5 minutes just to look at the available functions you should find ones that you could use.

 

EDIT: After thinking about this I can think of many different solutions. If you want help at least come up with something (an idea, a scrap of code, etc) to offer. I'm not going to do your homework for you, but I'll "help" you with it.

Link to comment
Share on other sites

I have tried array_unique but I am always getting error msgs

 

Ok, what error messages are you getting? I don't get any error messages - but I also don't get the results you are after. Because it is not the function you want to use. It would have been my first choice too and I probably would have tried it without reading the entire manual description for the function. But, as soon as it doesn't work, you should probably check the manual for any optional parameters, notes, etc. If you had, you would have seen this

    Note: Note that array_unique() is not intended to work on multi dimensional arrays.

 

So, you need a different approach. What functions/logic/approaches have you been covering? The assignments your instructor gives you usually requires something from what has been covered recently.

Link to comment
Share on other sites

You know, I just looked at the comments in the code. It is obvious you need to create a function (you're even given the function name to use) then it should be obvious you need to iterate through the array [use foreach()] and do a comparison on the name fields. I can understand if you get stuck, but you haven't even started with the easy stuff - well, at least you haven't shown any code to that effect.

Link to comment
Share on other sites

OK, here are some ideas on what I would do.

 

Create the function and create an empty output array. Then loop over each element in the input array. For each element in the array, check if there is a duplicate in the output array. If yes, skip to the next element (i.e. do nothing). however, if the entry doesn't exist in the output array - then add it. So, you will likely need nested foreach() loops - one for the input array, then another inside that to check the value against what is in the output.

Link to comment
Share on other sites

I'm wanting to do what you said. Make a new array, which I did. Then have a foreach in there for each item in the array and then have a conditional statement that says if there is not a duplicate of that entry to add it to the new array.

Link to comment
Share on other sites

Here's what I would do:

Since you're only concerned with the first and last names, pull them (concatenated) into a temporary array, using the key values of the parent array as the key values for the temp array.

 

array_unique($temp_array);

 

For each through the parent array, and if the key is not in the array keys of the temp array, unset it.

 

I've already coded this and it does work for what you've described, but try it and see if you can get it.

Link to comment
Share on other sites

I will try those out thanks. If I still can't get it to work then I'm just no good at setting up the syntax.

 

You still have not shown even one line of code. You know you need to iterate through he array so you should at least have a foreach() loop, right? Then add some code to test the data or repopulate into another array depending on the approach you are taking, then add the next step. Simply break down the solution into multiple steps. Then implement each steps and test that it works before adding the next step.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.