Reputation: 63
I need help with php array merging
I need merge by values,
so if I have values in array 1 like this
( room room ) and in array values ( room ) , merge will look like this = ( room room )
But if I have
( room room book ) and ( book book ) merged array values will look like this ( room room book book )
So lets say, merged array values will be maximum count of each element, room for example , in all arrays
Few more examples, ( book ) ( book ) ( book ) = ( book ) ( room book book ) ( library room ) = ( library room book book ) ( room room ) ( book book book ) = ( room room book book book ) (room) ( room ) ( room room room ) = ( room room room )
I tried operator + , but then
( room room ) ( book book book ) is ( room room book book ), that is not good
I tried array_merge , but then
( room ) ( room ) is ( room ) , this is okay but ( room room ) ( room ) is ( room room room ) and that is wrong
It doesnt depend on order, and keys are not imporant, it is always numeric ( [0] [1] etc. )
ARRAYS :
[0] => Array
(
[0] => room
[1] => room
)
[1] => Array
(
[0] => book
[1] => book
[2] => book
[3] => book
)
EXPECTED RESULT = array ( room, room, book , book , book, book )
[0] => Array
(
[0] => room
[1] => room
)
[1] => Array
(
[0] => room
)
EXPECTED RESULT = array ( room, room )
[0] => Array
(
[0] => room
)
[1] => Array
(
[0] => room
)
EXPECTED RESULT = array ( room )
[0] => Array
(
[0] => room
[1] => room
[2] => book
)
[1] => Array
(
[0] => room
[1] => book
)
[2] => Array
(
[0] => room
[1] => room
[2] => room
)
EXPECTED RESULT = array ( room , room ,room , book )
So it is alway maximum count of each ellement, for example book in all arrays
Thank you very much,
Marek
Upvotes: 0
Views: 1346
Reputation: 437376
This would be a reasonable way to do it in O(n):
function custom_array_merge() {
// variable number of inputs
$arrays = func_get_args();
// consolidate into array of items => count/array(of counts) across inputs
$counts = array_map('array_count_values', $arrays);
$consolidated = call_user_func_array('array_merge_recursive', $counts);
// reduce array of counts (where applicable) to maximum count
$maximums = array_map(function($v) {
return is_array($v) ? max($v) : $v;
}, $consolidated);
// build result -- could be faster with array_fill() and concatenating
// but that would definitely use up more memory
$result = [];
foreach ($maximums as $item => $times) {
for ($i = 0; $i < $times; ++$i) {
$result[] = $item;
}
}
return $result;
}
Upvotes: 0
Reputation: 346
Here it is:
function user_array_merge($arr) {
if (func_num_args() == 1) return $arr;
$vals = array_count_values($arr);
$arrs = func_get_args();
array_shift($arrs);
foreach ($arrs as $arr2) {
$vals2 = array_count_values($arr2);
foreach ($vals2 as $val=>$count) {
$vals[$val] = @max( $vals[$val], $count);
}
}
$result = array();
foreach ($vals as $val=>$count) {
$result = array_pad($result, count($result)+$count, $val);
}
return $result;
}
call like:
$res = user_array_merge(['room','room'], ['book', 'book','book','room'],['library']);
result:
var_export($res);
array (
0 => 'room',
1 => 'room',
2 => 'book',
3 => 'book',
4 => 'book',
5 => 'library',
)
Should be quite slow on large arrays.
Upvotes: 1