If someone has already done it, the idea is to merge different arrays into one and also merge the values sum
of price, qty
only for the identical keys.
In my example I only put two arrays to be merged $arr1, $arr2
but the idea is to do it dynamically because I do not have a defined number of arrays , I can have 2 as I can have 15.
Example:
<?php
$arr1 = [
51 => [
'name' => 'p1',
'price' => '20',
'qty' => '10',
'category' => 'c1'
],
3 => [
'name' => 'p2',
'price' => '10.50',
'qty' => '3',
'category' => 'c2'
]
];
$arr2 = [
3 => [
'name' => 'p3',
'price' => '23',
'qty' => '22',
'category' => 'c1'
],
102 => [
'name' => 'p4',
'price' => '10.50',
'qty' => '8',
'category' => 'c2'
]
];
Code :
mergeArrays(($arr1 $arr2));
function mergeArrays($array)
{
$mergedArray = [];
foreach ($array as $k => $arr) {
foreach ($arr as $key => $value) {
if (!isset($mergedArray[$key])) {
$mergedArray[$k][$key] = $value;
} else {
$mergedArray[$key] = $value;
}
}
}
print_r($mergedArray);
//return $mergedArray;
}
Result:
Array
(
[51] => Array
(
[name] => p1
[price] => 20
[qty] => 10
[category] => c1
)
[3] => Array
(
[name] => p2
[price] => 10.50
[qty] => 3
[category] => c2
)
[102] => Array
(
[name] => p4
[price] => 10.50
[qty] => 8
[category] => c2
)
)
Expected result:
Array
(
[51] => Array
(
[name] => p1
[price] => 20
[qty] => 10
[category] => c1
)
[3] => Array
(
[name] => p3 // the last erray value
[price] => 33.50 //sum
[qty] => 25 //sum
[category] => c1 // the last erray value
)
[102] => Array
(
[name] => p4
[price] => 10.50
[qty] => 8
[category] => c2
)
)
CodePudding user response:
Using the ...$in
to indicate any number of arrays can be passed to the function and then a couple of loops will do the job for you
$arr1 = [ 51 => [ 'name' => 'p1', 'price' => '20', 'qty' => '10', 'category' => 'c1' ],
3 => [ 'name' => 'p2', 'price' => '10.50', 'qty' => '3', 'category' => 'c2' ]
];
$arr2 = [ 3 => [ 'name' => 'p3', 'price' => '23', 'qty' => '22', 'category' => 'c1' ],
102 => [ 'name' => 'p4', 'price' => '10.50', 'qty' => '8', 'category' => 'c2' ]
];
function myMerge(Array ...$in)
{
$new = $in[0];
for($i=1; $i<count($in); $i ){
foreach($in[$i] as $idx => &$a) {
if( isset($new[$idx]) ){
$new[$idx]['qty'] = $a['qty'];
$new[$idx]['price'] = $a['price'];
} else {
$new[$idx] = $a;
}
}
}
return $new;
}
print_r( myMerge($arr1, $arr2) );
RESULT
Array
(
[51] => Array
(
[name] => p1
[price] => 20
[qty] => 10
[category] => c1
)
[3] => Array
(
[name] => p2
[price] => 33.5
[qty] => 25
[category] => c2
)
[102] => Array
(
[name] => p4
[price] => 10.50
[qty] => 8
[category] => c2
)
)
CodePudding user response:
hope this helps
<?php
$arr1 = [
51 => [
'name' => 'p1',
'price' => '20',
'qty' => '10',
'category' => 'c1'
],
3 => [
'name' => 'p2',
'price' => '10.50',
'qty' => '3',
'category' => 'c2'
]
];
$arr2 = [
3 => [
'name' => 'p3',
'price' => '23',
'qty' => '22',
'category' => 'c1'
],
102 => [
'name' => 'p4',
'price' => '10.50',
'qty' => '8',
'category' => 'c2'
]
];
$result = [];
foreach([$arr1, $arr2] as $arr) {
$keysFromCurrentArray = array_keys($arr);
foreach ($keysFromCurrentArray as $key) {
if(array_key_exists($key, $result)) {
$result[$key] = [
$result[$key]['name'] = $arr[$key]['name'],
$result[$key]['price'] = $arr[$key]['price'],
$result[$key]['qty'] = $arr[$key]['qty'],
$result[$key]['category'] = $arr[$key]['category'],
];
} else {
$result[$key] = $arr[$key];
}
}
}
var_export($result);
output :
array (
51 =>
array (
'name' => 'p1',
'price' => '20',
'qty' => '10',
'category' => 'c1',
),
3 =>
array (
0 => 'p3',
1 => 33.5,
2 => 25,
3 => 'c1',
),
102 =>
array (
'name' => 'p4',
'price' => '10.50',
'qty' => '8',
'category' => 'c2',
),
)
CodePudding user response:
Using array merge in loop is NOT optimal
You can put arrays in one like this:
$arrays = [ [1, 2], [2, 3], [5,8] ];
And then:
$merged = array_merge([], ...$arrays);