Page 1 of 1

How to add duplicates from one array to another array.

Posted: Wed Sep 12, 2007 4:31 pm
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 )
)

Posted: Wed Sep 12, 2007 4:38 pm
by feyd
Maybe a combination of array_unique() and array_diff()?

Posted: Wed Sep 12, 2007 5:45 pm
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.