Add up multidimensional Sub Array Values by other Sub Array Values - php

I am working on a statistics Dashboard where I want to display some stats sorted by calendar weeks.
I'm getting the data in a multidimensional Array with this format:
Array
(
[success] => 1
[stats] => Array
(
[0] => Array
(
[year] => 2018
[week] => 14
[sum] => 18
[country] => at
)
[1] => Array
(
[year] => 2018
[week] => 14
[sum] => 907
[country] => de
)
[2] => Array
(
[year] => 2018
[week] => 15
[sum] => 2
[country] =>
)
[3] => Array
(
[year] => 2018
[week] => 15
[sum] => 65
[country] => at
)
[4] => Array
(
[year] => 2018
[week] => 15
[sum] => 237
[country] => de
)
)
)
My Problem is, that it's not only grouped by calendar week, but by country as well. What I want to get is an Array, which is grouped by calendar week. The sum should be the sum of every sum value with the same week.
The Array I need should look like this one:
Array
(
[success] => 1
[stats] => Array
(
[0] => Array
(
[year] => 2018
[week] => 14
[sum] => 925
)
[1] => Array
(
[year] => 2018
[week] => 15
[sum] => 304
)
)
)
The country is not needed anymore and it should be only 1 entry for each calendar week.

Something like this should do the trick
$newArray = [];
foreach ($firstArray['stats'] as $entry) {
if (isset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])])) {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['sum'] += $entry['sum'];
} else {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])] = $entry;
}
}
EDIT
$newArray = [];
foreach ($stats['stats'] as $entry) {
if (isset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])])) {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['sum'] +=
$entry['sum'];
} else {
$newArray[sprintf('%d-%d', $entry['year'], $entry['week'])] = $entry;
}
unset($newArray[sprintf('%d-%d', $entry['year'], $entry['week'])]['country']);
}
$stats['stats'] = array_values($newArray);
#Rok D. Thanks for this one... I could adapt your example to perfectly fit my needs.

Related

Group array within group data php

After generating group array from an associative array, now the array is in the following format. And to make this group array I have used the following code:
foreach($upcoming_data_arr as $key => $item)
{
$arr[$item['year']][$key] = $item;
}
Array
(
[2018] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
[2] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[3] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
)
Now I need another group array according to month within year group. And in this case group data array will be like following:
Array
(
[2018] => Array
(
[11] => Array
(
[0] => Array
(
[id] => 6
[year] => 2018
[month] => 11
)
[1] => Array
(
[id] => 4
[year] => 2018
[month] => 11
)
[2] => Array
(
[id] => 3
[year] => 2018
[month] => 11
)
)
[12] => Array
(
[0] => Array
(
[id] => 5
[year] => 2018
[month] => 12
)
)
)
)
How we will get this output?
Add one more "dimension" when you are creating an array identified by $item['month'] and let php decide last "dimension" key by []:
foreach($upcoming_data_arr as $key => $item) {
$arr[$item['year']][$item['month']][] = $item;
}

calculate data within foreach loop

I have pulled out data from my database of eps of companies for last 2 years I wanted to sum up eps by using a small formula what happened here is I pulled out data in the form of array now I want to put formula like this `
`
$curr_eps - $old_eps / $old_eps * 100;
With the out put I am getting I am unable to put formula for every company and get separate calculated values
My output data is like this
foreach($data3 as $key => $pr_data) {
$prof_data[] = $pr_data;
}
Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
)
[2] => Array
(
[eps] => 0.90
[year] => 2015
)
[3] => Array
(
[eps] => 0.81
[year] => 2014
)
[4] => Array
(
[eps] => 1.05
[year] => 2016
)
[5] => Array
(
[eps] => 3.71
[year] => 2015
)
[6] => Array
(
[eps] => 1.61
[year] => 2016
)
[7] => Array
(
[eps] => -0.49
[year] => 2015
)
)
I am wondering to put data here as of without loop this is the output coming on
$prof_data[] = $data3 //This contains array value;
Out Put`
Array
(
[0] => Array
(
[0] => Array
(
[eps] => -0.28
[year] => 2015
[company_id] => 348
)
[1] => Array
(
[eps] => 3.33
[year] => 2014
[company_id] => 348
)
)
[1] => Array
(
[0] => Array
(
[eps] => 0.90
[year] => 2015
[company_id] => 351
)
[1] => Array
(
[eps] => 0.81
[year] => 2014
[company_id] => 351
)
)
[2] => Array
(
[0] => Array
(
[eps] => 1.05
[year] => 2016
[company_id] => 356
)
[1] => Array
(
[eps] => 3.71
[year] => 2015
[company_id] => 356
)
)
[3] => Array
(
[0] => Array
(
[eps] => 1.61
[year] => 2016
[company_id] => 366
)
[1] => Array
(
[eps] => -0.49
[year] => 2015
[company_id] => 366
)
)
[4] => Array
(
)
)
Now can anybody help me out to solve this issue I am not good while making to understand what I wam trying to do please let e know if you have queries
As I understand you can perform calculation as
foreach($data3 as $data){
$net = $currenteps - $data['eps'];
}
You haven't defined what are the currenteps and oldeps, however using $data['eps'] and $data['year'], you can access each eps value and respective year of array

how to arrange multidimensional array - Group by value in php?

I want to arrange same date items to single index, I have following array -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
)
)
[3] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 86649
[name] => item4
)
))
I want to arrange this array like -
Array(
[0] => Array
(
[date] => 30 Dec 2015
[record] => Array
(
[id] => 84675
[name] => Item1
)
)
[1] => Array
(
[date] => 28 Dec 2015
[record] => Array
(
[id] => 84675
[name] => item2
)
)
[2] => Array
(
[date] => 22 Nov 2015
[record] => Array
(
[id] => 2011
[name] => item3
),Array
(
[id] => 86649
[name] => item4
)
)
)
I want to arrange same date items into single index, can anybody please help me.I tried to arrange it using loops but couldn't get success.Any help would be really appreciated.
Thanks!
Try this code it can help
<?php
$result = array();
foreach($data as $info)
{
$result[$info['date']][] = $info;
}
print_r($result);
?>
You can do this in such a way also. In this way you will be able to get the dates in keys of $record_array and record values in values. Try this..
$your_array = array();
$record_array = array();
foreach ($your_array as $value){
$record_array[$value['date']][] = $value['record'];
}
print_r($record_array);
Hope this wil help

how to get value array using key from array

What I want to do
here is the main array
Array
(
[0] => Array
(
[Culture] => Array
(
[id] => 8
[title] => test123
[description] => test123
[year] => 2012
[photo] => test123.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[1] => Array
(
[Culture] => Array
(
[id] => 9
[title] => here title
[description] => here title
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[2] => Array
(
[Culture] => Array
(
[id] => 11
[title] => here title 2
[description] => here title 2
[year] => 2012
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[3] => Array
(
[Culture] => Array
(
[id] => 12
[title] => here title 3
[description] => here title 3
[year] => 2013
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[4] => Array
(
[Culture] => Array
(
[id] => 13
[title] => here title 4
[description] => here title 4
[year] => 2014
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
[5] => Array
(
[Culture] => Array
(
[id] => 14
[title] => here title 5
[description] => here title 5
[year] => 2015
[photo] => here.JPG
[datetime] => 0000-00-00 00:00:00
[status] => 0
)
)
)
now from this array I want array of year (by key)
like:
Array
(
[0]=>2012
[1]=>2013
[2]=>2014
[3]=>2015
)
Loop through your array and assign those years to a new array with their keys intact.
$years=array();
foreach($yourArray as $key=>$value)
{
$years[$key]=$value["Culture"]["year"];
}
$years = array_unique($years);
print_r($years);
You can first loop through the array with a foreach loop and then use array_unique to get the array of years:
$years = array();
foreach ($records as $record) {
$years[] = $record['Culture']['year'];
}
$years = array_unique($years);
Demo!
$years = [];
foreach($arr as $newarray){
$years[] = $newarray['Culture']['year'];
}
$years = array_unique($years);
The new array years will now hold all the years in the old array. array_unique will get rid of all duplicate years.
My way is to use array-walk that takes anonymous function to fill the array, the solution will be in only one line of code.
Its working for me
foreach($cultures as $row)
{
$year[]=$row['Culture']['year'];
}
$year = array_unique($year);
$year = array_values($year);
echo "<pre>";print_r($year);

MSSQL / PHP - assistance with query and resultset to get data for bar chart

I am trying to create a bar chart that shows monthly hits for the past 12 months.
I have the below query:
SELECT
(MONTH(date)) AS MONTH
, (YEAR(date)) AS YEAR
, SUM(hits) AS count
FROM [statistics]
WHERE ID = '". $id ."'
AND (date > DATEADD(yy, - 1, GETDATE()))
GROUP BY (MONTH(date)), (YEAR(date))
ORDER BY year, month
This provides the below resultset:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
)
This only shows the months that it has results for but I need all months whether they have results or not (so count would be 0). What I need is something more like the below which includes all 12 months:
Array
(
[0] => Array
(
[Admin] => Array
(
[month] => 7
[year] => 2012
[count] => 702
)
)
[1] => Array
(
[Admin] => Array
(
[month] => 8
[year] => 2012
[count] => 650
)
)
[2] => Array
(
[Admin] => Array
(
[month] => 9
[year] => 2012
[count] => 670
)
)
[3] => Array
(
[Admin] => Array
(
[month] => 10
[year] => 2012
[count] => 66
)
)
[4] => Array
(
[Admin] => Array
(
[month] => 11
[year] => 2012
[count] => 53
)
)
[5] => Array
(
[Admin] => Array
(
[month] => 12
[year] => 2012
[count] => 39
)
)
[6] => Array
(
[Admin] => Array
(
[month] => 1
[year] => 2013
[count] => 54
)
)
[7] => Array
(
[Admin] => Array
(
[month] => 2
[year] => 2013
[count] => 36
)
)
[8] => Array
(
[Admin] => Array
(
[month] => 3
[year] => 2013
[count] => 48
)
)
[9] => Array
(
[Admin] => Array
(
[month] => 4
[year] => 2013
[count] => 0
)
)
[10] => Array
(
[Admin] => Array
(
[month] => 5
[year] => 2013
[count] => 0
)
)
[11] => Array
(
[Admin] => Array
(
[month] => 6
[year] => 2013
[count] => 0
)
)
)
Can anyone suggest which would be the best way to achieve this? Could it be done in the SQL or does it need to be done in the code? If so, how?
Many thanks in advance.
This can be achieved through PHP using a loop which iterates all the possible months, and if there is data for that month, add it to an output array, otherwise, insert a blank entry with count of 0 for that month
EDIT
Example solution;
$m = $resultset[0]['Admin']['month'];
$y = $resultset[0]['Admin']['year'];
for ($i = 0; $i < 12; $i++) {
$adj = ((($i - 1) + $m) % 12) + 1;
if (!(isset($resultset[$i]['Admin']['month']) && $resultset[$i]['Admin']['month'] == $adj)) {
array_splice($resultset, $i, 0, array(array(
'Admin' => array(
'month' => $adj,
'year' => $y + ($i + $m > 12),
'count' => 0
)
)));
}
}
The important part for solving your issue is the line;
$adj = ((($i - 1) + $m) % 12) + 1;
This translates $i from being an integer between 0-11 to a month between 1-12, depending on the starting month ($m)
Well try this code it may help
foreach ($array as $year => $values) {
for ($i = 1; $i <= 12; $i++) { // Loop through months 1 .. 12
if (!$values[$i]) {
echo "0";
} else {
echo $values[$i];
}
}
}

Categories