PHP Merge Array(S) and Delete Double Values

PHP - Merging two arrays into one array (also Remove Duplicates)

array_unique(array_merge($array1,$array2), SORT_REGULAR);

http://se2.php.net/manual/en/function.array-unique.php

PHP merge array(s) and delete double values

The following should do the trick.

$flattened = array_unique(call_user_func_array('array_merge', $therapie));

or the more efficient alternative (thanks to erisco's comment):

$flattened = array_keys(array_flip(
call_user_func_array('array_merge', $therapie)
));

If $therapie's keys are strings you can drop array_unique.

Alternatively, if you want to avoid call_user_func_array, you can look into the various different ways of flattening a multi-dimensional array. Here are a few (one, two) good questions already on SO detailing several different methods on doing so.

I should also note that this will only work if $therapie is only ever a 2 dimensional array unless you don't want to flatten it completely. If $therapie is more than 2 dimensions and you want to flatten it into 1 dimension, take a look at the questions I linked above.

Relevant doc entries:

array_flip
array_keys
array_merge
array_unique
call_user_func_array

Remove duplicate values from php array and merge quantity

Here is a solution :

$products_list[] = [
'manufacturer' => 'manufacturer1',
'mpn' => 'mpn1',
'description' => 'desc',
'quantity' => 2,
'condition' => 'condition',
'price' => 10
];

$products_list[] = [
'manufacturer' => 'manufacturer1',
'mpn' => 'mpn1',
'description' => 'desc',
'quantity' => 3,
'condition' => 'condition',
'price' => 10
];

$products_list[] = [
'manufacturer' => 'manufacturer2',
'mpn' => 'mpn2',
'description' => 'desc2',
'quantity' => 4,
'condition' => 'condition2',
'price' => 15
];

$quantities = [];

foreach ( $products_list as $product ) {
$key = $product['description'].'|'.$product['condition']; // fields you want to compare
if ( !isset($quantities[$key]) ) {
$quantities[$key] = $product;
} else {
$quantities[$key]['quantity'] += $product['quantity'];
}
}

$products = array_values($quantities);

print_r($products);

And the result is

Array ( [0] => Array ( [manufacturer] => manufacturer1 [mpn] => mpn1 [description] => desc [quantity] => 5 [condition] => condition [price] => 10 ) [1] => Array ( [manufacturer] => manufacturer2 [mpn] => mpn2 [description] => desc2 [quantity] => 4 [condition] => condition2 [price] => 15 ) )

Issue in merge array and delete duplicate all array

You could do something like the following: first get all the user_ids that we do not want to be included, by taking the intersection of the two columns.
If we then merge the two separate arrays and exclude the ones that are present in the array of unwanted IDs we should get our final result.

$userId = 'user_id';

$userIdsArray1 = array_column($array1, $userId);
$userIdsArray2 = array_column($array2, $userId);

// If we then take the negation we should get something similar to an exclusive or.
// Thus those in one or the other but not in both arrays.
$unwantedUserIds = array_intersect($userIdsArray1, $userIdsArray2);

$result = [];

foreach (array_merge($array1, $array2) as $record) {
if (!in_array($record[$userId], $unwantedUserIds)) {
$result[] = $record;
}
}

echo '<pre>', print_r($result, true), '</pre>';

Result:

Array
(
[0] => Array
(
[id] => 2
[user_id] => 3
[name] => demo_test1
[profilePhoto] =>
)

[1] => Array
(
[id] => 4
[user_id] => 4
[name] => test
[profilePhoto] =>
)

)

Merge two arrays containing objects and remove duplicate values

You can combine the array_merge() function with the array_unique() function (both titles are pretty self-explanatory)

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

How to remove duplicate values from a multi-dimensional array in PHP

Here is another way. No intermediate variables are saved.

We used this to de-duplicate results from a variety of overlapping queries.

$input = array_map("unserialize", array_unique(array_map("serialize", $input)));


Related Topics



Leave a reply



Submit