Yure Posted October 28, 2011 Share Posted October 28, 2011 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 } ?> Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2011 Share Posted October 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
Yure Posted October 28, 2011 Author Share Posted October 28, 2011 I have tried array_unique but I am always getting error msgs Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2011 Share Posted October 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2011 Share Posted October 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
Yure Posted October 28, 2011 Author Share Posted October 28, 2011 ya because im just not too had at doing this sort of thing, im going to try some foreach and see if I can't compare each of the values, I just don't know how I would format it exactly Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 28, 2011 Share Posted October 28, 2011 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. Quote Link to comment Share on other sites More sharing options...
Yure Posted October 28, 2011 Author Share Posted October 28, 2011 that makes sense, thanks I will try it and let you know how it goes Quote Link to comment Share on other sites More sharing options...
Yure Posted October 29, 2011 Author Share Posted October 29, 2011 Well I still can't get it to work and I think it's because I'm not too had at syntax. I know what I want to do but I don't know how to set it up properly so that it triggers. Quote Link to comment Share on other sites More sharing options...
Yure Posted October 29, 2011 Author Share Posted October 29, 2011 meant to say not to good at the syntax. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 29, 2011 Share Posted October 29, 2011 Well, explain what you want to do and I'll provide assistance Quote Link to comment Share on other sites More sharing options...
Yure Posted October 30, 2011 Author Share Posted October 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2011 Share Posted October 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 30, 2011 Share Posted October 30, 2011 For each through the parent array, and if the key is not in the array keys of the temp array, unset it. Or just use array_intersect_key() Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 30, 2011 Share Posted October 30, 2011 Never noticed that one. Quote Link to comment Share on other sites More sharing options...
Yure Posted October 31, 2011 Author Share Posted October 31, 2011 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. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 31, 2011 Share Posted October 31, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.