Re: [PHP] Unique items in an array
Hi folks, I just wanted to add a little something to this thread as I reworked some of the code and came across an interesting tidbit. I was trying to merge many result sets which now had the table's primary key as the array key, but found that when array_merge() was done, all of the array's keys were lost, like so: array(2) { [704]= array(36) { [contact_id]= string(3) 704 [contact_first_name]= string(4) Marc [contact_last_name]= string(4) Guay } [705]= array(36) { [contact_id]= string(3) 705 [contact_first_name]= string(4) Marc [contact_last_name]= string(5) Guay2 } } array_merge with: array(0) { } left me with: array(2) { [0]= array(36) { [contact_id]= string(3) 704 [contact_first_name]= string(4) Marc [contact_last_name]= string(4) Guay } [1]= array(36) { [contact_id]= string(3) 705 [contact_first_name]= string(4) Marc [contact_last_name]= string(5) Guay2 } } Pretty crummy. Apparently what I wanted was as simple as: $new_array = $array1 + $array2, which I didn't even know was an option. Thank you php.net/manual. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
Assuming you want to make things unique based on the contact_first_name field, how would you decide which record to keep? The first one you run in to, the last one you come across, or some other criteria? The unique field is actually the contact_id. Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
On 12/15/2011 6:24 AM, Marc Guay wrote: Assuming you want to make things unique based on the contact_first_name field, how would you decide which record to keep? The first one you run in to, the last one you come across, or some other criteria? The unique field is actually the contact_id. Marc Marc, If that is the case, can you explain to us how you got the two entries in the array to begin with? If this is from a DB query, then it should probably be address else where. If it was loaded from a text file, it should be handled differently. No matter how it was created, you could always build a method (if the dataset isn't too large) that would filter things out. Given your example array, I would do something like this: ?php $oldDataSet = array( array( contact_id = 356, contact_first_name = Marc, ), array( contact_id = 247, contact_first_name = Marc, ), array( contact_id = 356, contact_first_name = Marc, ), ); $newDataSet = array(); foreach ( $oldDataSet AS $k = $entry ) { $newDataSet[$entry['contact_id']] = $entry; unset($oldDataSet[$k]); } print_r($newDataSet); ? This would result in something like this: Array ( [356] = Array ( [contact_id] = 356 [contact_first_name] = Marc ) [247] = Array ( [contact_id] = 247 [contact_first_name] = Marc ) ) Give it a try, should do what you are wanting. -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
Give it a try, should do what you are wanting. Hi Jim, I appreciate your dedication to this problem but it was solved 2 days ago! :) Thanks Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
On 12/13/2011 1:15 PM, Marc Guay wrote: Hi folks, Let's say that I have the following array: [0]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } [1]= array(35) { [contact_id]= string(3) 247 [contact_first_name]= string(4) Marc } [2]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } And I would like to filter out exact duplicates, such as key 0 and key 2 in this example, leaving me with an array containing only unique entries. How would you go about it? Thanks for any help, Marc Assuming you want to make things unique based on the contact_first_name field, how would you decide which record to keep? The first one you run in to, the last one you come across, or some other criteria? -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/ http://www.bendsource.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
On Tue, 2011-12-13 at 16:15 -0500, Marc Guay wrote: Hi folks, Let's say that I have the following array: [0]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } [1]= array(35) { [contact_id]= string(3) 247 [contact_first_name]= string(4) Marc } [2]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } And I would like to filter out exact duplicates, such as key 0 and key 2 in this example, leaving me with an array containing only unique entries. How would you go about it? Thanks for any help, Marc If I knew exactly what each sub-array were to contain (with regards to keys) and it was small enough, I could compare those. For your example, I would imagine just looping through and comparing the contact_id value, as that seems to be what you'd consider the primary key in database terms. If the array elements were a bit more unknown, and you needed to check for exact duplicates, because for example, the forename (contact_first_name) changed, then you could serialise the array elements and then compare those. I don't know if serialising the arrays in this context would alter if the natural index of an array differed though without testing (and I'm too lazy to do that for you ;-p ) -- Thanks, Ash http://www.ashleysheridan.co.uk
Re: [PHP] Unique items in an array
On Tue, Dec 13, 2011 at 10:15 PM, Marc Guay marc.g...@gmail.com wrote: Hi folks, Let's say that I have the following array: [0]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } [1]= array(35) { [contact_id]= string(3) 247 [contact_first_name]= string(4) Marc } [2]= array(35) { [contact_id]= string(3) 356 [contact_first_name]= string(4) Marc } And I would like to filter out exact duplicates, such as key 0 and key 2 in this example, leaving me with an array containing only unique entries. How would you go about it? Thanks for any help, Marc If the contact_id is the primary key, you could also use that as array index, which would automatically filter duplicates. If you want to filter out duplicate names after that, you could use array_unique for that. Matijn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
If the contact_id is the primary key, you could also use that as array index, which would automatically filter duplicates. Thanks for this, it's so obvious I didn't see it. I was adding items to the array with $array[] = $contact when I could have just as easily used $array[$contact['id']] = $contact; Cheerios, Marc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Unique items in an array
Marc Guay wrote: If the contact_id is the primary key, you could also use that as array index, which would automatically filter duplicates. Thanks for this, it's so obvious I didn't see it. I was adding items to the array with $array[] = $contact when I could have just as easily used $array[$contact['id']] = $contact; The other question might be where are you getting the information from ;) I filter the data in the database before loading the array ... -- Lester Caine - G8HFL - Contact - http://lsces.co.uk/wiki/?page=contact L.S.Caine Electronic Services - http://lsces.co.uk EnquirySolve - http://enquirysolve.com/ Model Engineers Digital Workshop - http://medw.co.uk// Firebird - http://www.firebirdsql.org/index.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php