Counting unserialized array grouped by index - php

I have a serialized array in my database that I have extracted and unserialized. It has a structure like this:
array (
526744 =>
array (
'completed' => 13,
'total' => 24,
'topics' =>
array (
),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
)
The first number is a course id (there are actually 3 courses I've just included the first here).
I have pulled this out of the database for several different users, so I have the above several times over.
I am trying to count the lessons, so that I know how many users have passed each lesson.
I have the following code:
foreach($results as $result) {
$course_progress = unserialize($result->course_progress);
$lesson_progress = $course_progress[$course_id][lessons];
print_r(array_count_values($lesson_progress));
}
This outputs this:
Array ( [1] => 24 )
Array ( [1] => 11 [0] => 13 )
Array ( [1] => 13 [0] => 11 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 23 [0] => 1 )
Array ( [1] => 24 )
Array ( [1] => 21 [0] => 3 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [1] => 24 )
Array ( [0] => 21 [1] => 3 )
This is obviously completely wrong. But I cannot think of how to get it to work. I think the issue is that I have a separate array for each user with my current code maybe?
Any help would be greatly appreciated.

Attempt 4:
$users = array(
'john' => array(
526744 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
526745 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 1,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
),
'joe' => array(
526744 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 0,
526753 => 0,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
526745 => array (
'completed' => 13,
'total' => 24,
'topics' =>
array (),
'lessons' =>
array (
526745 => 1,
526747 => 1,
526749 => 1,
526751 => 1,
526753 => 1,
526755 => 0,
526757 => 0,
526759 => 0,
526761 => 1,
),
'last_id' => 526793,
),
)
);
$counts = [];
foreach($users as $userId => $userCourses){
foreach($userCourses as $courseId => $course){
foreach($course['lessons'] as $lessonId => $lesson){
$counts[$courseId][$lessonId] = empty($counts[$courseId][$lessonId]) ? $lesson : $counts[$courseId][$lessonId]+$lesson;
}
}
}
var_dump($counts);
Obviously My array keys (john and joe) are just for testing purposes so the outermost foreach is negligible.
Returns:
array (size=2)
526744 =>
array (size=9)
526745 => int 2
526747 => int 2
526749 => int 2
526751 => int 0
526753 => int 0
526755 => int 0
526757 => int 0
526759 => int 0
526761 => int 2
526745 =>
array (size=9)
526745 => int 2
526747 => int 2
526749 => int 2
526751 => int 1
526753 => int 2
526755 => int 0
526757 => int 0
526759 => int 0
526761 => int 2

Related

Returning Multiple Values of the same id in array

I'm pulling information from 3 different tables in MSSQL 2008 and I'd like to get the SUM of CC_qty as well as each Location condensed into one field per id. If this can be done in the query itself that would be fantastic - listagg and GROUP_CONCAT are not cutting it. Otherwise I've been working with array_reduce, array_merge, array_diff to no avail.
Here is my query and the original array:
SELECT a.id, a.qty, b.locationID, b.CC_qty, c.Location FROM (
SELECT left(id, 10) as id, MAX(qty) as qty
FROM db1
WHERE id like 'abc-abc%'
GROUP BY left(id, 10)
) as a
JOIN (
SELECT locationID, left(SKU, 10) as SKU, CC_qty FROM db2
WHERE CC_qty > 25
) as b on a.abc-abc = b.SKU
JOIN (
SELECT locationID, Location FROM db3
) as c on b.locationID = c.locationID
Array
(
[0] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 276
[CC_qty] => 250
[Location] => NOP11
)
[1] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 310
[CC_qty] => 1385
[Location] => NOP01
)
[2] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 84
[CC_qty] => 116
[Location] => NOP06
)
[3] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 254
[CC_qty] => 432
[Location] => NOP08
)
[4] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 228
[CC_qty] => 101
[Location] => NOP04
)
[5] => Array
(
[id] => abc-abc-34
[qty] => 0
[locationID] => 254
[CC_qty] => 436
[Location] => NOP08
)
[6] => Array
(
[id] => abc-abc-34
[qty] => 0
[locationID] => 254
[CC_qty] => 62
[Location] => NOP08
)
[7] => Array
(
[id] => abc-abc-45
[qty] => 0
[locationID] => 75
[CC_qty] => 89
[Location] => NOP05
)
[8] => Array
(
[id] => abc-abc-45
[qty] => 0
[locationID] => 202
[CC_qty] => 372
[Location] => NOP07
)
)
This is my desired output, for simplicity of knowing what information I absolutely require I've removed qty and locationID but those don't have to be removed:
Array
(
[0] => Array
(
[id] => abc-abc-12
[CC_qty] => 1635
[Location] => NOP11, NOP01
)
[1] => Array
(
[id] => abc-abc-23
[CC_qty] => 649
[Location] => NOP06, NOP08, NOP04
)
[2] => Array
(
[id] => abc-abc-34
[CC_qty] => 495
[Location] => NOP08
[3] => Array
(
[id] => abc-abc-45
[CC_qty] => 461
[Location] => NOP05, NOP07
)
)
Thanks for looking!
Being that I left an answer for MySQL, it wasn't going to work for this. I don't know MSSQL well enough to use it, so here's a way to do it with PHP so I don't leave you completely without an answer.
$arr = array
(
array
(
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => 276,
'CC_qty' => 250,
'Location' => 'NOP11'
),
array
(
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => 310,
'CC_qty' => 1385,
'Location' => 'NOP01'
),
array
(
'id' => 'abc-abc-23',
'qty' => 0,
'locationID' => 84,
'CC_qty' => 116,
'Location' => 'NOP06'
)
);
$combinedArr = array();
foreach ($arr as $a)
{
$found = false;
foreach ($combinedArr as $i => $b)
{
if ($b['id'] == $a['id'])
{
$found = true;
$locs = explode(',', $a['Location']);
$combinedArr[$i]['CC_qty'] += $a['CC_qty'];
if (!in_array($b['Location'], $locs))
{
$locs[] = $b['Location'];
$combinedArr[$i]['Location'] = implode(', ', $locs);
}
}
}
if (!$found)
$combinedArr[] = $a;
}
print_r($combinedArr);
/*
Array
(
[0] => Array
(
[id] => abc-abc-12
[qty] => 0
[locationID] => 276
[CC_qty] => 1635
[Location] => NOP01, NOP11
)
[1] => Array
(
[id] => abc-abc-23
[qty] => 0
[locationID] => 84
[CC_qty] => 116
[Location] => NOP06
)
)
*/
I don't have any experience with MSSQL, but I feel rather confident that it provides the necessary functionality to merge, sum, and concatenate. Anyhow, I am compelled to post an answer because I find the answer from Thomas to be unrefined.
Essentially, you should use the id values as temporary keys to determine if you are processing the first occurrence of the group or a subsequent occurrence. On the first encounter, just save the whole row to the output array. For all future rows belonging to the same group, just sum and concatenate the desired values.
To remove the temporary keys in the result array, just call array_values($result).
Code: (Demo)
$array = [
['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 276, 'CC_qty' => 250, 'Location' => 'NOP11'],
['id' => 'abc-abc-12', 'qty' => 0, 'locationID' => 310, 'CC_qty' => 1385, 'Location' => 'NOP01'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 84, 'CC_qty' => 116, 'Location' => 'NOP06'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 432, 'Location' => 'NOP08'],
['id' => 'abc-abc-23', 'qty' => 0, 'locationID' => 228, 'CC_qty' => 101, 'Location' => 'NOP04'],
['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 436, 'Location' => 'NOP08'],
['id' => 'abc-abc-34', 'qty' => 0, 'locationID' => 254, 'CC_qty' => 62, 'Location' => 'NOP08'],
['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 75, 'CC_qty' => 89, 'Location' => 'NOP05'],
['id' => 'abc-abc-45', 'qty' => 0, 'locationID' => 202, 'CC_qty' => 372, 'Location' => 'NOP07'],
];
$result = [];
foreach ($array as $row) {
if (!isset($result[$row['id']])) {
$result[$row['id']] = $row;
} else {
$result[$row['id']]['qty'] += $row['qty']; // SUM
$result[$row['id']]['locationID'] .= ", " . $row['locationID']; // CONCAT
$result[$row['id']]['CC_qty'] += $row['CC_qty']; // SUM
$result[$row['id']]['Location'] .= ", " . $row['Location']; // CONCAT
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'id' => 'abc-abc-12',
'qty' => 0,
'locationID' => '276, 310',
'CC_qty' => 1635,
'Location' => 'NOP11, NOP01',
),
1 =>
array (
'id' => 'abc-abc-23',
'qty' => 0,
'locationID' => '84, 254, 228',
'CC_qty' => 649,
'Location' => 'NOP06, NOP08, NOP04',
),
2 =>
array (
'id' => 'abc-abc-34',
'qty' => 0,
'locationID' => '254, 254',
'CC_qty' => 498,
'Location' => 'NOP08, NOP08',
),
3 =>
array (
'id' => 'abc-abc-45',
'qty' => 0,
'locationID' => '75, 202',
'CC_qty' => 461,
'Location' => 'NOP05, NOP07',
),
)

Sort array by mutiple fields (closest to number)

How can I sort a array by two(one) different values?
So I have a array like this:
array(
array(
'id' => 10,
'total' => 38,
'entry' => 400
),
array(
'id' => 4,
'total' => 34,
'entry' => 3100
),
array(
'id' => 2,
'total' => 34,
'entry' => 3150
),
array(
'id' => 8,
'total' => 34,
'entry' => 2980
),
);
The array is already sorted by the key total, but they all have the same value in total. So I need to sort by who is closest to 3000 by entry.
Edit
The array should be first sorted by total and then entry, since entry is only there so I can differentiate who is the best.
So the array should look like this:
array(
array(
'id' => 10,
'total' => 38,
'entry' => 400
),
array(
'id' => 8,
'total' => 34,
'entry' => 2980
),
array(
'id' => 4,
'total' => 34,
'entry' => 3100
),
array(
'id' => 2,
'total' => 34,
'entry' => 3150
)
);
Try this:
usort($arr, function ($a, $b) {
if ($a['total'] == $b['total']) { // Only compare on entry when the totals are the same.
return abs($a['entry'] - 3000) > abs($b['entry'] - 3000);
}
return $a['total'] < $b['total'];
});
print_r($arr);
Output:
Array
(
[0] => Array
(
[id] => 2
[total] => 35
[entry] => 3150
)
[1] => Array
(
[id] => 8
[total] => 34
[entry] => 2980
)
[2] => Array
(
[id] => 4
[total] => 34
[entry] => 3100
)
[3] => Array
(
[id] => 6
[total] => 34
[entry] => 3250
)
[4] => Array
(
[id] => 3
[total] => 32
[entry] => 3400
)
)
Here's how it works: it compares the totals, but if they're the same, it compares the absolute value of the difference between entry and 3000 of both entrys.
eval.in demo

How to append this array as it is in php

I need to append this array in php script
.Kindly help to do it perfectly .I tried multiple solution but none resulted to correct solution.
Array(
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
)
to the following array
Array(
[Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
//add the given array here
))
So that the result is
Array([Don] => Array
(
[Ab] => 1
[Ang] => 1
[Ant] => 2
[Bo] => 1
[Ch] => 1
[Chri] => 2
[Chri] => 4
[Deau] => 1
[Der] => 1
[Sylveste] => 1
)
[Lam] => Array
(
[Ab] => 2
[Ch] => 22
[Dona] => 1
[Irw] => 1
[Kou] => 1
[Llo] => 1
[Ro] => 1
[Shumy] => 1
)
[Oti] => Array
(
[Ab] => 1
[Arla] => 1
[Kour] => 1
[Osh] => 1
[Roy ] => 1
[Tim] => 1
[War] => 1
[Donn] => 0
[Lamo] => 0
[Otis] => 0
[Stev] => 0
[Matt] => 0
[Samm] => 0
[Andr] => 0
[Jerr] => 0
[Simm] => 0
[Steph] => 0
[Fredd] => 0
[Willi] => 0
//added here
))
Please use least loops as much as possible.So as to get time feasible solution.Use php array function if possible.
Use foreach to add into the oti index:
<?php
$firstArr = Array(
"Don" => Array
(
"Ab" => 1,
"Ang" => 1,
"Ant" => 2,
"Bo" => 1,
"Ch" => 1,
"Chri" => 2,
"Chri" => 4,
"Deau" => 1,
"Der" => 1,
"Sylveste" => 1,
),
"Lam" => Array
(
"Ab" => 2,
"Ch" => 22,
"Dona" => 1,
"Irw" => 1,
"Kou" => 1,
"Llo" => 1,
"Ro" => 1,
"Shumy" => 1,
),
"Oti" => Array
(
"Ab" => 1,
"Arla" => 1,
"Kour" => 1,
"Osh" => 1,
"Roy " => 1,
"Tim" => 1,
"War" => 1,
//add the given array here
));
$secondArr = Array(
"Donn" => 0,
"Lamo" => 0,
"Otis" => 0,
"Stev" => 0,
"Matt" => 0,
"Samm" => 0,
"Andr" => 0,
"Jerr" => 0,
"Simm" => 0,
"Steph" => 0,
"Fredd" => 0,
"Willi" => 0,
);
foreach ($secondArr as $key => $value) {
$firstArr["Oti"][$key] = $value;
}
print_r($firstArr);
Demo
You can also use array_merge as #Mark Baker said in comments:
$firstArr["Oti"] = array_merge($firstArr["Oti"], $secondArr);
This should solve it, you use the array_merge function to merge the 2 arrays :)
$secondArray['Oti'] = array_merge($secondArray['Oti'], $firstArray);

how to display multidimensional array without knowing depth?

I have one large multidimensional array(I dnt the depth of array) and I want to display it in tabular form . this is one array from my array
[Kai Roger Tester] => Array
(
[Ikke navngitt] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 3
[waiting_approval_documents] => 1
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[Finnfjord] => Array
(
[NVD test] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 5
)
)
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 2
)
)
[Endringslogg] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[Laste opp doc] => Array
(
[status] => Array
(
[documents_green] => 0
[documents_yellow] => 0
[documents_red] => 1
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 1
)
)
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 6
[waiting_approval_documents] => 3
[waiting_verfication_documents] => 4
[under_construction_documents] => 13
)
)
[Prosess 1] => Array
(
[AF Decom] => Array
(
[status] => Array
(
[documents_green] => 1
[documents_yellow] => 0
[documents_red] => 0
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 3
)
)
[status] => Array
(
[documents_green] => 7
[documents_yellow] => 0
[documents_red] => 2
[waiting_approval_documents] => 0
[waiting_verfication_documents] => 0
[under_construction_documents] => 11
)
)
Folder Name green yellow red
Kai Roger Tester 1 0 6
Ikke navngitt' 0 0 3
Finnfjord 0 0 0
NVD test 0 0 1
Process1 1 1 0
I tired with below method
public static function getfoldertable($array, $prefix = '') {
$body_start = "<tbody>";
if (count($array) > 0 && is_array($array)) {
$i = 0;
foreach ($array as $key => $row) {
$body_start.='<tr>
<td>'.$i.'</td>
<td>'.$key.'</td>
<td>'.$row['status']['documents_green'].'</td>
<td>'.$row['status']['documents_yellow'].'</td>
<td>'.$row['status']['documents_red'].'</td>
</tr>';
$body_start.= self::getfoldertable($row, $prefix . '-');
$i++;
}
$body_start.="</tbody>";
}
//echo $body_start; die;
return $body_start;
}
Can anyone help me how can i display this?
Thanks in advance
Here is a function you could use for generating that output:
function getTreeHTML($tree, $level = 0) {
$html = "";
$indent = str_repeat(" ", $level * 2);
foreach ($tree as $key => $value) {
if ($key == "status") continue;
$html .= "
<tr><td>$indent$key</td>
<td>{$value['status']['documents_green']}</td>
<td>{$value['status']['documents_yellow']}</td>
<td>{$value['status']['documents_red']}</td>
</tr>" . getTreeHTML($value, $level+1);
}
return $html;
}
If the input data is defined like this:
$input = array (
'Kai Roger Tester' =>
array (
'Ikke navngitt' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 3,
'waiting_approval_documents' => 1,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'Finnfjord' =>
array (
'NVD test' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 5,
),
),
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 2,
),
),
'Endringslogg' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'Laste opp doc' =>
array (
'status' =>
array (
'documents_green' => 0,
'documents_yellow' => 0,
'documents_red' => 1,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 1,
),
),
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 6,
'waiting_approval_documents' => 3,
'waiting_verfication_documents' => 4,
'under_construction_documents' => 13,
),
),
'Prosess 1' =>
array (
'AF Decom' =>
array (
'status' =>
array (
'documents_green' => 1,
'documents_yellow' => 0,
'documents_red' => 0,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 3,
),
),
'status' =>
array (
'documents_green' => 7,
'documents_yellow' => 0,
'documents_red' => 2,
'waiting_approval_documents' => 0,
'waiting_verfication_documents' => 0,
'under_construction_documents' => 11,
),
),
);
...and you would call that function like this:
$html = getTreeHTML($input);
...embedding that HTML result as follows in a table:
<table border=1>
<tr><th>Folder Name</th><th>green</th><th>yellow</th><th>red</th></tr>
<?=$html?>
</table>
...then the output would look in a browser like this:
Use RecursiveIterator.
Working code:
$array = array();
$array[0] = array('test' => array('asdf', 'asdf', array('asf', array('asdfads', 'asdf' => array(234,234,234)))));
$iterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator($array),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $key => $item) {
if (is_array($item)) {
echo '<pre>';print_r($item);echo '</pre>';
}
}

Summing The Contents Of A Three Tiered Array PHP

This is the array I am using (3 tiers), and I am trying to add the number of sales (nosales) from the first month in the first array to the first month in the second array (Should be 150) and so on through all the variables (months shouldn't be added) so I am left with a two level array with the nosales, salevalue,salecost and saleprofit totals for each month.
Array (
[0] => Array
(
[1] => Array
(
[month] => 1
[nosales] => 100
[salevalue] => 1200
[salecost] => 360
[saleprofit] => 840
)
[2] => Array
(
[month] => 2
[nosales] => 110
[salevalue] => 1320
[salecost] => 396
[saleprofit] => 924
)
)
[1] => Array
(
[1] => Array
(
[month] => 1
[nosales] => 50
[salevalue] => 350
[salecost] => 70
[saleprofit] => 280
)
[2] => Array
(
[month] => 2
[nosales] => 55
[salevalue] => 385
[salecost] => 77
[saleprofit] => 308
)
)
)
Now, I have tried looping through them to get them to add together but I am getting a number of errors. Could someone please help?
Here is the script I am using at the moment:
$acc = array_shift($results_array);
foreach ($results_array as $val) {
foreach ($val as $v) {
foreach ($v as $key => $v){
$acc[$key] += $v;
}
}
}
Thanks for your help in advance!
Is this what you wanted to do?
$aResultsArray = array(
0 => array(
1 => array(
'month' => 1,
'nosales' => 100,
'salevalue' => 1200,
'saleconst' => 360,
'saleprofit' => 840,
),
2 => array(
'month' => 2,
'nosales' => 110,
'salevalue' => 1320,
'saleconst' => 396,
'saleprofit' => 924,
),
),
1 => array(
1 => array(
'month' => 1,
'nosales' => 50,
'salevalue' => 350,
'saleconst' => 70,
'saleprofit' => 280,
),
2 => array(
'month' => 2,
'nosales' => 55,
'salevalue' => 385,
'saleconst' => 77,
'saleprofit' => 308,
),
),
);
$aSum = array();
foreach ($aResultsArray as $mYear => $aMonths) {
foreach ($aMonths as $mMonth => $aMonth) {
if (!isset($aSum[$aMonth['month']])) {
$aSum[$aMonth['month']] = array(
'month' => $aMonth['month'],
'nosales' => 0,
'salevalue' => 0,
'saleconst' => 0,
'saleprofit' => 0,
);
}
$aSum[$aMonth['month']]['nosales'] += $aMonth['nosales'];
$aSum[$aMonth['month']]['salevalue'] += $aMonth['salevalue'];
$aSum[$aMonth['month']]['saleconst'] += $aMonth['saleconst'];
$aSum[$aMonth['month']]['saleprofit'] += $aMonth['saleprofit'];
}
}
var_dump($aSum);

Categories