tags:

views:

314

answers:

5

I have two arrays that I would like to compare and ultimately wind up with a single array with everything combined, having no duplicates. Can someone please tell me which function I should use? There are so many that it's a bit confusing.

$array1[]['name'] = 'Kim, Jones';
$array1[]['name'] = 'Jim, Miller';

array1 is an array I built that I want added to an array coming from the database. The key in the second array is also named "name". Thanks.

EDIT:

I managed to merge these two arrays but I can still see duplicates.

This is what the first array looks like:

Array
(
    [0] => Array
        (
            [WNumber] => ADMIN
            [Name] => Tim, Cooley
            [Employer] => CalPERS
            [Student] => 1
            [Perm] => 1
            [QA] => 0
            [Supervisor] => 1
            [RQW] => 0
        )

My second array is built like this:

$add_names[]['Name']='Jim, Jones';

I just want to add $add_names to the first array WHERE there are no duplicates.

A: 

Does php's array_diff() do what you want?

http://us.php.net/manual/en/function.array-diff.php

or more likely array_diff_assoc:

http://us.php.net/manual/en/function.array-diff-assoc.php

JasonWoof
A: 

Does array_diff work for you?

Description

array array_diff ( array $array1 , array $array2 [, array $ ... ] )
Compares array1 against array2 and returns the difference.
Preet Sangha
Hi Preet, thanks for the help. I don't see how I can use array_diff because I used array_merge on the two arrays and am left with a single array. What would I use as a second array to get the diff?
A: 

I think you'll need to use a combination of array_merge (adds the two arrays together) and array_unique (removes duplicate values).

$resulting_array = array_unique(array_merge($array1, $array2));

Note that array_unique will not work correctly when using multi-dimensional arrays, so if your array data looks the way you put it in your question, you'll have to think of a way around that. One of the comments on the array_unique page suggests serialize'ing all array values before running array_unique on it. Afterwards you'd just run unserialize on all array elements. Note that this can mean a performance hit if you have a big array, so you might want to consider avoiding multi-dimensional arrays in this scenario.

Something like this:

$merged_array = array_merge($array1, $array2);
$serialized_array = array_map("serialize", $merged_array);
$filtered_array = array_unique($serialized_array);
$final_array = array_map("unserialize", $filtered_array);
Aistina
Thanks Aistina but that gives me the first item in the array and nothing else.
I just refreshed and now see the comment you added. I only have 100 items in the array. The first array from my database has multiple key value pairs but I'm only concerned about the name value and have built the second array to match.
Thanks Aistina. Let give this a try and I will be right back and let you know how it goes.
Aistina, I still am getting the combined arrays but I have duplicates. Is there any way to get rid of these?
+1  A: 

I'm tempted to sell you on CakePHP, since it has a number of functions that makes this easy in its "Set" class. Your problem is that you have the results in a nested array. A simple "array_unique" does not work in a nested array.

I'd do it the old fashioned way...

$array1[]['name'] = 'Kim, Jones';
$array1[]['name'] = 'Jim, Miller';

$array2[]['name'] = 'Kim, Jones';
$array2[]['name'] = 'Jimbo, Miller';
$array2[]['name'] = 'Jim, Jones';

$new_array=array_merge($array1, $array2);
$out_array = array();
$key_array = array();
foreach($new_array as $i => $row) {
        if (empty($key_array[$row['name']])) {
                $out_array[] = $row;
        }
        $key_array[$row['name']] = 1;
}

print_r($out_array);

This code works for me...

Dooltaz
Hi Dooltaz. I'm going to give that a try now. Thanks a bunch! I was also thinking bout using a foreach with a comparison. brb
Hey Dooltaz. Thanks a bunch!! It worked great. Thank you again!
A: 

There isn't a direct function for handling what you are looking for in php, probably you need to write a function for it.

What I understood from your question is that you have 2 arrays :

$a = array( array( 'name' => 'Omid' ), 12 );
$b = array( array( 'name' => 'testing' ) );

and you want to merge them to get

$merge = array( array( 'name' => 'testing' ), 12 );

if that's what you want then you might want to take a look at this comment array merge recursive which leads to this code :

function array_merge_recursive_distinct ( array &$array1, array &$array2 )
{
  $merged = $array1;

  foreach ( $array2 as $key => &$value )
  {
    if ( is_array ( $value ) && isset ( $merged [$key] ) && is_array ( $merged [$key] )     )
    {
      $merged [$key] = array_merge_recursive_distinct ( $merged [$key], $value );
    }
    else
    {
      $merged [$key] = $value;
    }
  }

  return $merged;
}
Cynics
Hi Cynics. Thanks for the code snippet. I'm going to try this. brb