Reputation: 425
I need to merge and group the data from 3 multidimensional arrays by a shared date column. Within these groups, I want to sum the Unit column values so that I have a single 2d array with rows containing unique dates and total units.
$array1 = [
[
"sales" => [
["Date" => "2014-04-01", "Units" => 1],
["Date" => "2014-04-02", "Units" => 7]
]
]
];
$array2 = [
[
"sales" => [
["Date" => "2014-04-01", "Units" => 3],
["Date" => "2014-04-02", "Units" => 2]
]
]
];
$array3 = [
[
"sales" => [
["Date" => "2014-04-01", "Units" => 0],
["Date" => "2014-04-02", "Units" => 5]
]
]
];
Desired output::
[
['Date' => '2014-04-01', 'Units' => 4],
['Date' => '2014-04-02', 'Units' => 14],
]
Upvotes: -1
Views: 68
Reputation: 47864
Merge the relevant subsets from each array into a single array so that all data can be iterated in a single loop.
While looping, if a date value is encountered for the first time, save the whole row as a reference variable and push that reference into the result array. If a date value is encountered again, access only Units value and add it to the reference's Unit value.
Using references in this way avoids needing to call array_values()
after looping to remove temporary keys.
Code: (Demo)
$result = [];
foreach (array_merge($a[0]['sales'], $b[0]['sales'], $c[0]['sales']) as $row) {
if (!isset($ref[$row['Date']])) {
$ref[$row['Date']] = $row;
$result[] =& $ref[$row['Date']];
} else {
$ref[$row['Date']]['Units'] += $row['Units'];
}
}
var_export($result);
Upvotes: 0
Reputation: 126
Assuming you have the arrays as you mention, a way to do it is the following:
<?php
$first = array ();
$first[0] = array ( "Date" => "2014-04-01", "Units" => 1 );
$first[1] = array ( "Date" => "2014-04-02", "Units" => 7 );
$first[2] = array ( "Date" => "2014-04-03", "Units" => 5 );
$first[3] = array ( "Date" => "2014-04-04", "Units" => 1 );
echo "<pre>";
print_r ($first);
echo "</pre>";
echo "<hr>";
$second = array ();
$second[0] = array ( "Date" => "2014-04-01", "Units" => 3 );
$second[1] = array ( "Date" => "2014-04-02", "Units" => 2 );
$second[2] = array ( "Date" => "2014-04-03", "Units" => 5 );
$second[3] = array ( "Date" => "2014-04-04", "Units" => 2 );
echo "<pre>";
print_r ($second);
echo "</pre>";
echo "<hr>";
$third = array ();
$third[0] = array ( "Date" => "2014-04-01", "Units" => 0 );
$third[1] = array ( "Date" => "2014-04-02", "Units" => 5 );
$third[2] = array ( "Date" => "2014-04-03", "Units" => 5 );
$third[3] = array ( "Date" => "2014-04-04", "Units" => 4 );
echo "<pre>";
print_r ($third);
echo "</pre>";
echo "<hr>The total is: ";
$total = array();
foreach ($first as $key=>$day) {
$total[$key]=array("Date" => "2014-04-02", "Units" => ($first[$key]["Units"] +$second[$key]["Units"] +$third[$key]["Units"]) );
}
echo "<pre>";
print_r ($total);
echo "</pre>";
echo "<hr>";
But this works only if the key for each date is the same. (Aka, 0 always corresponds to 2014-04-01 ). If I had to work with that, I would probably look one step before, to how those arrays get created, and I would use the date, as the key
Upvotes: 0
Reputation: 1166
<?php
$arr1=array(
'Date' => '2014-04-02'
'Units' => 5
);
$arr2=array(
'Date' => '2014-04-02'
'Units' => 5
);
$arr3=array(
'Date' => '2014-04-02'
'Units' => 5
);
$arr['sales'][0]=$arr1;
$arr['sales'][1]=$arr2;
$arr['sales'][2]=$arr3;
$data[]=$arr;
print_r($data);
?>
Upvotes: 0
Reputation: 667
<?php
$array1 = array("orange", "banana");
$array2 = array("ab", "ba");
$array3 = array("cd", "ef");
array_push($array1,$array2 , $array3);
print_r($array1);
?>
Upvotes: 1