How to add duplicates from one array to another array.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
buddyq
Forum Newbie
Posts: 5
Joined: Wed Sep 12, 2007 4:23 pm

How to add duplicates from one array to another array.

Post by buddyq »

Okay,

I have an associative array called $isPrinted that has some duplicate information.

I need to make another array that holds all the duplicates of $isPrinted. I only need the duplicates of $isPrinted added once to the new array.

How do I do this? I need this for comparison later on in the code.

As an example:

the $isPrinted array is:

Array (
[0] => Array ( [section] => 2 [courseNum] => 9 )
[1] => Array ( [section] => 1 [courseNum] => 3 )
[2] => Array ( [section] => 1 [courseNum] => 7 )
[3] => Array ( [section] => 2 [courseNum] => 3 )
[4] => Array ( [section] => 2 [courseNum] => 10)
[5] => Array ( [section] => 3 [courseNum] => 9 )
[6] => Array ( [section] => 1 [courseNum] => 3 ) /*duplicate*/ This should be added to the new array
[7] => Array ( [section] => 1 [courseNum] => 3 ) /*duplicate*/ only once.
)



I need the new array to contain only duplicates of $isPrinted. So the new array should read:

Array (
[0] => Array ( [section] => 1 [courseNum] => 3 )
)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Maybe a combination of array_unique() and array_diff()?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Most questions like this can usually be solved at the source (how the array is created) instead of creating the array, and then going back and fixing it.
Post Reply