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
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] =>
)
)
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 ) )
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));
PHP Merge two arrays without duplicates
With the examples you've given, I can think up something like this:
$array = array();
foreach ($Fields1 as $row) {
if (!isset($array[$row['UniqueID']])) {
$array[$row['UniqueID']] = $row;
} else {
if (!is_null($row['WaterHot'])) {
$array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
}
if (!is_null($row['WaterCold'])) {
$array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
}
}
}
foreach ($Fields2 as $row) {
if (!is_null($row['WaterHot'])) {
$array[$row['UniqueID']]['WaterHot'] = $row['WaterHot'];
}
if (!is_null($row['WaterCold'])) {
$array[$row['UniqueID']]['WaterCold'] = $row['WaterCold'];
}
}
It's pretty straight forward, and merge the WaterHot and WaterCold fields, where the UniqueID column is the unique key.
The UniqueID column will also be the key of the merged array. If you don't want this, and want a numeric column, you'll need to use array_values
on the resulting array.
Related Topics
Import CSV File Directly into MySQL
PHP Fatal Error: Call to Undefined Function Json_Decode()
Difference Between 2 Dates in Seconds
Upgrading PHP in Xampp For Windows
Warning: Domdocument::Loadhtml(): Htmlparseentityref: Expecting ';' in Entity,
Why Are $_Post Variables Getting Escaped in PHP
Best Way to Check If MySQL_Query Returned Any Results
Smtp Connect() Failed. Message Was Not Sent.Mailer Error: Smtp Connect() Failed
What Is the Syntax For Accessing PHP Object Properties
"Vertical" Regex Matching in an Ascii "Image"
PHP-Intl Installation on Xampp
Remove Warning Messages in PHP
Can't Access Global Variable Inside Function
Detect Exact Os Version from Browser
How to Hide/Encode/Encrypt PHP Source Code and Let Others Have the System