Merge two 2d arrays and remove duplicate rows - php

I searched a lot of SOF threads and no one seems to stick to my problem. What's kind of wired because this should be a well discussed question :)
Maybe I'm seeking for the wrong thing...
Scenario:
I have 2 arrays
$a = [
['id' => 5, 'name' => 'bruce'],
['id' => 7, 'name' => 'wayne']
];
// 2 elements
and
$b = [
['id' => 6, 'name' => 'chuck'],
['id' => 8, 'name' => 'norris'],
['id' => 7, 'name' => 'wayne'] //also exists in array $a
];
// 3 elements
My goal is
$c = [
['id' => 6, 'name' => 'chuck'],
['id' => 8, 'name' => 'norris'],
['id' => 7, 'name' => 'wayne'],
['id' => 5, 'name' => 'bruce']
];
// 4 elements (no duplicates)
I really don't care about the order inside the array(s) but I want to merge both into one, without having duplicates.
I tried array_merge and array_merge_recursive. No one works. Probably because the functions doesn't know the identifier which identifies each entry. Is there an easy solution or do I really have to create an own method/function for this?
Maybe there is a closure that I could use?

You can do this with very simple inbuilt function of PHP
$c = array_unique(array_merge($a,$b), SORT_REGULAR);
print_r( $c )
The output of the print_r is
Array
(
[0] => Array
(
[id] => 5
[name] => bruce
)
[1] => Array
(
[id] => 7
[name] => wayne
)
[2] => Array
(
[id] => 6
[name] => chuck
)
[3] => Array
(
[id] => 8
[name] => norris
)
)

$temp = array_merge($b, $a);
foreach ($temp as $v) {
$c[$v['id']] = $v;
}
If it finds the same id, the element will be overwritten in $c

Here's an approach at hashing each array with serialize after a key sort:
<?php
$a = [
['id' => 5, 'name' => 'bruce'],
['id' => 7, 'name' => 'wayne']
];
$b = [
['id' => 6, 'name' => 'chuck'],
['name' => 'wayne', 'id' => 7],
['id' => 8, 'name' => 'norris']
];
$merged = array_merge($a, $b);
foreach($merged as $k => $v) {
ksort($v);
$hashes[$k] = serialize($v);
}
$hashes = array_unique($hashes);
var_export(array_intersect_key($merged, $hashes));
Output:
array (
0 =>
array (
'id' => 5,
'name' => 'bruce',
),
1 =>
array (
'id' => 7,
'name' => 'wayne',
),
2 =>
array (
'id' => 6,
'name' => 'chuck',
),
4 =>
array (
'id' => 8,
'name' => 'norris',
),
)

If you index them on unique id then just add them. The result will be indexed on id which is convenient:
$result = array_column($a, null, 'id') + array_column($b, null, 'id');

I don't know how performant this is, but just using phps array-manipulation functions I get:
>>> array_values(array_merge(array_combine(array_column($a, 'name'), $a), array_combine(array_column($b, 'name'), $b)));
=> [
[
"id" => 5,
"name" => "bruce",
],
[
"id" => 7,
"name" => "wayne",
],
[
"id" => 6,
"name" => "chuck",
],
[
"id" => 8,
"name" => "norris",
],
]

Related

Sort associative array by values based on another array [duplicate]

This question already has answers here:
Sort multidimensional array based on another array [duplicate]
(3 answers)
Closed 26 days ago.
Hi I have an array that looks like this
array[
0 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 3
],
1 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 15
],
2 => array[
'id' => 1,
'name' => 'Test 1'
'classId' => 17
],
]
And I have another array that contains classIds like:
classIds = [15, 17, 3]
And I want to sort my array based on classIds
I can do a a double loop to compare it. I am just wondering is there anyother way to get it done?
Actually one loop i enough:
<?php
$order = [15, 17, 3];
$input = [
[
'id' => 1,
'name' => 'Test 1',
'classId' => 3,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 15,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 17,
],
];
$output = [];
array_walk($input, function($entry) use ($order, &$output) {
$output[array_search($entry['classId'], $order)] = $entry;
});
ksort($output);
print_r($output);
In case you consider array_search(...) also a "loop" (though it internally works different), that would be an alternative which produces the same output:
<?php
$order = array_flip([15, 17, 3]);
$input = array_values([
[
'id' => 1,
'name' => 'Test 1',
'classId' => 3,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 15,
],
[
'id' => 1,
'name' => 'Test 1',
'classId' => 17,
],
]);
$output = [];
array_walk($input, function($entry, $index) use ($order, &$output) {
$output[$order[$entry['classId']]] = $entry;
});
ksort($output);
print_r($output);
The output of both approaches is:
Array
(
[0] => Array
(
[id] => 1
[name] => Test 1
[classId] => 15
)
[1] => Array
(
[id] => 1
[name] => Test 1
[classId] => 17
)
[2] => Array
(
[id] => 1
[name] => Test 1
[classId] => 3
)
)
You can sort the array directly and in-place using usort and an anonymous comparison function that retrieves the target indices from $classIds.
Given
<?php
$arr = array(
0 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 3
),
1 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 15
),
2 => array(
'id' => 1,
'name' => 'Test 1',
'classId' => 17
),
);
$classIds = [15, 17, 3];
you can sort $arr with
// Create assoc array mapping classIds to their desired sorted position.
$classIdsOrder = array_combine($classIds, range(0, count($classIds)-1));
// Sort $arr according to the 'classId' order described by $classIdsOrder
usort($arr, function ($left, $right) use ($classIdsOrder) {
return $classIdsOrder[$left['classId']] <=> $classIdsOrder[$right['classId']];
});
print_r($arr);
which outputs
Array
(
[0] => Array
(
[id] => 1
[name] => Test 1
[classId] => 15
)
[1] => Array
(
[id] => 1
[name] => Test 1
[classId] => 17
)
[2] => Array
(
[id] => 1
[name] => Test 1
[classId] => 3
)
)
Try it online!

How to remove from a multidimensional array all duplicate elements including the original?

I am using php 7.1.
I have seen that to eliminate the duplicate elements it is enough with this
array_unique($array, SORT_REGULAR);
I've also seen this work
array_map("unserialize", array_unique(array_map("serialize", $array)));
But that only deletes the elements that are duplicated from the array, I want to delete those that are duplicated but I don't want it to leave me only 1 without a duplicate, I want it to also delete that original on which it has been based to verify that it is duplicated
How could I do it?
For example i have this
$array = array(
[0] = array(
[id] => 1,
[number] => 12345,
[date] => 2022-05-09
)
[1] = array(
[id] => 2,
[number] => 123456,
[date] => 2022-05-09
)
[2] = array(
[id] => 3,
[number] => 123456,
[date] => 2022-05-09
)
[3] = array(
[id] => 3,
[number] => 123456,
[date] => 2022-05-09
)
)
How can i let it become this:?
$array = array(
[0] = array(
[id] => 1,
[number] => 12345,
[date] => 2022-05-09
)
[1] = array(
[id] => 2,
[number] => 123456,
[date] => 2022-05-09
)
)
This should be straightforward. Pluck all IDs using array_column and use array_count_values to get counts of occurrences of each ID. Then, use array_filter to filter only unique ones.
<?php
$unique_ids = array_count_values(array_column($array,'id'));
$res = array_filter($array, fn($v) => $unique_ids[$v['id']] === 1);
print_r($res);
Online Demo
Implementing the advice from How to remove values from an array if occurring more than one time?, for best time complexity, keep a lookup array of previously encountered values and their index. When a value is encountered more than once, delete the current and original row from the input array. It is perfectly safe to call unset() on an element that has already been unset(), no breakage will occur.
I have extended your input array to demonstrate that unset() will not cause trouble. I am using "array destructuring" in the foreach() to make the code more concise.
Code: (Demo)
$array = [
['id' => 1, 'number' => 12345, 'date' => '2022-05-09'],
['id' => 2, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 3, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 3, 'number' => 123456, 'date' => '2022-05-09'],
['id' => 4, 'number' => 123457, 'date' => '2022-05-10'],
['id' => 4, 'number' => 123458, 'date' => '2022-05-11'],
['id' => 3, 'number' => 123459, 'date' => '2022-05-12']
];
$found = [];
foreach ($array as $index => ['id' => $id]) {
if (isset($found[$id])) {
unset($array[$index], $array[$found[$id]]);
} else {
$found[$id] = $index;
}
}
var_export($array);
Output:
array (
0 =>
array (
'id' => 1,
'number' => 12345,
'date' => '2022-05-09',
),
1 =>
array (
'id' => 2,
'number' => 123456,
'date' => '2022-05-09',
),
)

Sort A Multi Dimensional Array In Laravel [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed last year.
This is the result of dd() that I use in my controller on laravel 8. I want to sort the data based on the JB column. I can't use the manual order by in SQL syntax because I get JB from complex DB RAW. Therefore I want to sort this multi-dimensional array using php. Does anyone know how to sort the multi-dimensional array based on JB column value?
here I go...
you can use array_multisort PHP function. Link
$new = [
[
'id' => 13,
'name' => 'Tony',
'jb' => 3,
],
[
'id' => 15,
'name' => 'Joe',
'jb' => 2,
],
[
'id' => 16,
'name' => 'Ross',
'jb' => 1,
],
[
'id' => 18,
'name' => 'Monika',
'jb' => 5,
],
[
'id' => 20,
'name' => 'Joye',
'jb' => 7,
],
];
$keys = array_column($new, 'jb');
array_multisort($keys, SORT_ASC, $new);
so as a result you will get link,
Array
(
[0] => Array
(
[id] => 16
[name] => Ross
[jb] => 1
)
[1] => Array
(
[id] => 15
[name] => Joe
[jb] => 2
)
[2] => Array
(
[id] => 13
[name] => Tony
[jb] => 3
)
[3] => Array
(
[id] => 18
[name] => Monika
[jb] => 5
)
[4] => Array
(
[id] => 20
[name] => Joye
[jb] => 7
)
)
sortBy method solves your issue. Laravel really has a strong bunch of methods in collections. Below code block shows an example.
$collection = collect([
['name' => 'Desk', 'price' => 200],
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
]);
$sorted = $collection->sortBy('price');
$sorted->values()->all();
/*
[
['name' => 'Chair', 'price' => 100],
['name' => 'Bookcase', 'price' => 150],
['name' => 'Desk', 'price' => 200],
]
*/

Merging arrays based on a value of the key [duplicate]

This question already has answers here:
Merge arrays of associative arrays by shared column values [duplicate]
(3 answers)
Closed 5 months ago.
I have two arrays of arrays that have an id key, and I'd like to merge the data together based on that array's key and key value. The data would look something like:
$color = [
['id' => 1, 'color' => 'red'],
['id' => 2, 'color' => 'green'],
['id' => 3, 'color' => 'blue'],
];
$size = [
['id' => 1, 'size' => 'SM'],
['id' => 2, 'size' => 'XL'],
['id' => 3, 'size' => 'MD'],
['id' => 4, 'size' => 'LG'],
];
$combined = [
['id' => 1, 'color' => 'red', 'size' => 'SM'],
['id' => 2, 'color' => 'green', 'size' => 'XL'],
['id' => 3, 'color' => 'blue', 'size' => 'MD'],
['id' => 4, 'size' => 'LG'],
];
Is there a particularly efficient function or trick for handling something like this? Or should I just loop through the elements of one array and push the contents to the other?
I'm also using Laravel, and the data is a result of an eloquent query, so I can also utilize the collections if it would make the code cleaner.
Use array_replace_recursive function for easy and fast way
array_replace_recursive($color, $size)
Pure php solution is to use array_replace_recursive like this:
array_replace_recursive(
array_combine(array_column($color, "id"), $color),
array_combine(array_column($size, "id"), $size)
);
You should notice that array_replace_recursive merge arrays by keys.
So, if you get such data from database:
$color = [
['id' => 1, 'color' => 'red'],
['id' => 2, 'color' => 'red']
];
$size = [
['id' => 2, 'size' => 'SM']
];
array_replace_recursive will return corrupted merge:
$combined = [
['id' => 2, 'color' => 'red', 'size' => 'SM'],
['id' => 2, 'color' => 'red']
];
The solution is to combine array_replace_recursive with array_column and array_combine for merging arrays by their's id field:
array_replace_recursive(
array_combine(array_column($color, "id"), $color),
array_combine(array_column($size, "id"), $size)
);
array_combine(array_column($color, "id"), $color) creates associative array with id as keys.
So, in your case it will return:
$combined = [
1 => ['id' => 1, 'color' => 'red', 'size' => 'SM'],
2 => ['id' => 2, 'color' => 'green', 'size' => 'XL'],
3 => ['id' => 3, 'color' => 'blue', 'size' => 'MD'],
4 => ['id' => 4, 'size' => 'LG'],
];
You can use array_replace_recursive to merge the arrays in your particular situation.
$color = array(
array('id' => 1, 'color' => 'red'),
array('id' => 2, 'color' => 'green'),
array('id' => 3, 'color' => 'blue'),
);
$size = array(
array('id' => 1, 'size' => 'SM'),
array('id' => 2, 'size' => 'XL'),
array('id' => 3, 'size' => 'MD'),
array('id' => 4, 'size' => 'LG'),
);
$merged = array_replace_recursive($color, $size);
Output:
array(4) {
[0]=>
array(3) {
["id"]=>
int(1)
["color"]=>
string(3) "red"
["size"]=>
string(2) "SM"
}
[1]=>
array(3) {
["id"]=>
int(2)
["color"]=>
string(5) "green"
["size"]=>
string(2) "XL"
}
[2]=>
array(3) {
["id"]=>
int(3)
["color"]=>
string(4) "blue"
["size"]=>
string(2) "MD"
}
[3]=>
array(2) {
["id"]=>
int(4)
["size"]=>
string(2) "LG"
}
}
Note: I used the traditional array layout because my PHP version won't support the new one yet :)
Second option
You can also use array_map. This will let you add as much arrays as you want with a little tweaking.
$merged = array_map(function ($c, $s) {
return array_merge($c, $s);
}, $color, $size);
var_dump($merged); // See output above
I'd suggest using laravel's collections, since this question has the laravel tag.
$color = collect(
['id' => 1, 'color' => 'red'],
['id' => 2, 'color' => 'green'],
['id' => 3, 'color' => 'blue']
);
$size = collect(
['id' => 1, 'size' => 'SM'],
['id' => 2, 'size' => 'XL'],
['id' => 3, 'size' => 'MD'],
['id' => 4, 'size' => 'LG']
);
$combined = $color->merge($size);
Folllow this:
array_replace_recursive() is recursive : it will recurse into arrays and apply the same process to the inner value.
$combined = array_replace_recursive($color, $size);
then you can print to see the result as bellow:
print_r($combined);
Simple nested loop would solve the purpose.
foreach($size as $key => $value1) {
foreach($color as $value2) {
if($value1['id'] === $value2['id']){
$size[$key]['color'] = $value2['color'];
}
}
}
echo '<pre>';
print_r($size);
Output:
Array
(
[0] => Array
(
[id] => 1
[size] => SM
[title] => red
)
[1] => Array
(
[id] => 2
[size] => XL
[title] => green
)
[2] => Array
(
[id] => 3
[size] => MD
[title] => blue
)
[3] => Array
(
[id] => 4
[size] => LG
)
)
You need to traverse all elements from both arrays one by one and merge any duplicate elements. To do this you need to perform these steps.
Glue both arrays together.
$arr = [
['id' => 1, 'color' => 'red'],
['id' => 2, 'color' => 'green'],
['id' => 3, 'color' => 'blue'],
['id' => 1, 'size' => 'SM'],
['id' => 2, 'size' => 'XL'],
['id' => 4, 'size' => 'LG'],
['id' => 3, 'size' => 'MD'],
];
Loop the combined array copying the items into a new array.
If the ID has been seen before don't append the row, but merge it with an existing one.
You can achieve all of this with a single foreach loop and array_merge. There is no need for recursive functions or nested loops.
// Loops on merged arrays and copied elements into a new array
foreach(array_merge($color, $size) as $el){
// If the element with this id is already in new array then add the elements together
$merged[$el['id']] = ($merged[$el['id']] ?? []) + $el;
}
The only downside is that you lose the original indexes, but it looks from your question that this was not important to you. If you want, you can reindex the array with
$merged = array_values($merged);
Live Demo
Try:
$out = array();
foreach ($size as $key => $value){
if(!isset($color[$key])) { $color[$key] = array(); }
$out[] = array_merge((array)$value,(array)$color[$key]);
}
Output:
Array
(
[0] => Array
(
[id] => 1
[size] => SM
[color] => red
)
[1] => Array
(
[id] => 2
[size] => XL
[color] => green
)
[2] => Array
(
[id] => 3
[size] => MD
[color] => blue
)
[3] => Array
(
[id] => 4
[size] => LG
)
)
Better way using loop. First calculate the max array the by count this number run a loop. It will work.

PHP - How to merge arrays inside array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
How to merge n number of array in php. I mean how can I do the job like :
array_merge(from : $result[0], to : $result[count($result)-1])
OR
array_merge_recursive(from: $result[0], to : $result[count($result) -1])
Where $result is an array with multiple arrays inside it like this :
$result = Array(
0 => array(),//associative array
1 => array(),//associative array
2 => array(),//associative array
3 => array()//associative array
)
My Result is :
$result = Array(
0 => Array(
"name" => "Name",
"events" => 1,
"types" => 2
),
1 => Array(
"name" => "Name",
"events" => 1,
"types" => 3
),
2 => Array(
"name" => "Name",
"events" => 1,
"types" => 4
),
3 => Array(
"name" => "Name",
"events" => 2,
"types" => 2
),
4 => Array(
"name" => "Name",
"events" => 3,
"types" => 2
)
)
And what I need is
$result = Array(
"name" => "name",
"events" => array(1,2,3),
"types" => array(2,3,4)
)
array_merge can take variable number of arguments, so with a little call_user_func_array trickery you can pass your $result array to it:
$merged = call_user_func_array('array_merge', $result);
This basically run like if you would have typed:
$merged = array_merge($result[0], $result[1], .... $result[n]);
Update:
Now with 5.6, we have the ... operator to unpack arrays to arguments, so you can:
$merged = array_merge(...$result);
And have the same results. *
* The same results as long you have integer keys in the unpacked array, otherwise you'll get an E_RECOVERABLE_ERROR : type 4096 -- Cannot unpack array with string keys error.
I really liked the answer from complex857 but it didn't work for me, because I had numeric keys in my arrays that I needed to preserve.
I used the + operator to preserve the keys (as suggested in PHP array_merge with numerical keys) and used array_reduce to merge the array.
So if you want to merge arrays inside an array while preserving numerical keys you can do it as follows:
<?php
$a = [
[0 => 'Test 1'],
[0 => 'Test 2', 2 => 'foo'],
[1 => 'Bar'],
];
print_r(array_reduce($a, function ($carry, $item) { return $carry + $item; }, []));
?>
Result:
Array
(
[0] => Test 1
[2] => foo
[1] => Bar
)
If you would like to:
check that each param going into array_merge is actually an array
specify a particular property within one of the arrays to merge by
You can use this function:
function mergeArrayofArrays($array, $property = null)
{
return array_reduce(
(array) $array, // make sure this is an array too, or array_reduce is mad.
function($carry, $item) use ($property) {
$mergeOnProperty = (!$property) ?
$item :
(is_array($item) ? $item[$property] : $item->$property);
return is_array($mergeOnProperty)
? array_merge($carry, $mergeOnProperty)
: $carry;
}, array()); // start the carry with empty array
}
Let's see it in action.. here's some data:
Simple structure: Pure array of arrays to merge.
$peopleByTypesSimple = [
'teachers' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
],
'students' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
],
'parents' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
],
];
Less simple: Array of arrays, but would like to specify the people and ignore the count.
$peopleByTypes = [
'teachers' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Ms. Jo', 'hair_color' => 'brown'],
1 => (object) ['name' => 'Mr. Bob', 'hair_color' => 'red'],
]
],
'students' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Joey', 'hair_color' => 'blonde'],
1 => (object) ['name' => 'Anna', 'hair_color' => 'Strawberry Blonde'],
]
],
'parents' => [
'count' => 2,
'people' => [
0 => (object) ['name' => 'Mr. Howard', 'hair_color' => 'black'],
1 => (object) ['name' => 'Ms. Wendle', 'hair_color' => 'Auburn'],
]
],
];
Run it
$peopleSimple = mergeArrayofArrays($peopleByTypesSimple);
$people = mergeArrayofArrays($peopleByTypes, 'people');
Results - Both return this:
Array
(
[0] => stdClass Object
(
[name] => Ms. Jo
[hair_color] => brown
)
[1] => stdClass Object
(
[name] => Mr. Bob
[hair_color] => red
)
[2] => stdClass Object
(
[name] => Joey
[hair_color] => blonde
)
[3] => stdClass Object
(
[name] => Anna
[hair_color] => Strawberry Blonde
)
[4] => stdClass Object
(
[name] => Mr. Howard
[hair_color] => black
)
[5] => stdClass Object
(
[name] => Ms. Wendle
[hair_color] => Auburn
)
)
Extra Fun:
If you want to single out one property in an array or object, like "name" from an array of people objects(or associate arrays), you can use this function
function getSinglePropFromCollection($propName, $collection, $getter = true)
{
return (empty($collection)) ? [] : array_map(function($item) use ($propName) {
return is_array($item)
? $item[$propName]
: ($getter)
? $item->{'get' . ucwords($propName)}()
: $item->{$propName}
}, $collection);
}
The getter is for possibly protected/private objects.
$namesOnly = getSinglePropFromCollection('name', $peopleResults, false);
returns
Array
(
[0] => Ms. Jo
[1] => Mr. Bob
[2] => Joey
[3] => Anna
[4] => Mr. Howard
[5] => Ms. Wendle
)
Try this
$result = array_merge($array1, $array2);
Or, instead of array_merge, you can use the + op which performs a union:
$array2 + array_fill_keys($array1, '');

Categories