Group array within group data php - 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;
}

Related

php convert same ids in multidimensional array to count from 0

well this must a easy one but can't seem to figure it out I have this field range in an array that prints an id which repeats it self on array and would like to convert it to a sequence count starting from 0.
Live example: https://3v4l.org/Sf4nI#v7.0.33
Current output:
Array
(
[0] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 336
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 390
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 390
[year] => 2021
[month] => 222
)
)
Output I need:
Array
(
[0] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[1] => Array
(
[range] => 0
[year] => 2020
[month] => 222
)
[2] => Array
(
[range] => 1
[year] => 2020
[month] => 222
)
[3] => Array
(
[range] => 1
[year] => 2021
[month] => 222
)
)
I think the simple solution here is to extract all range values from the array and remove the duplicated values, to make it start from 0 we will use array_values and flip it to can get the id by range value
$idList = array_flip(array_values(array_unique(array_column($array, 'range'))));
now you can get the id for any range you want like this
$idList[336];
// or
$idList[390];
https://3v4l.org/kUa8D#v7.0.33
hope it's helpful

sorting by key in multi dimensional array [duplicate]

This question already has answers here:
PHP sort array by two field values [duplicate]
(6 answers)
Closed 5 years ago.
Array
(
[0] => Array
(
[sales] => 117513.00000000
[month] => 1
[month_name] => January
[year] => 2018
)
[1] => Array
(
[purchases] => 136350.00000000
[month] => 9
[month_name] => September
[year] => 2017
)
[2] => Array
(
[sales] => 23025.00000000
[month] => 11
[month_name] => November
[year] => 2017
)
[3] => Array
(
[sales] => 6447.00000000
[month] => 12
[month_name] => December
[year] => 2017
)
)
Here I already sorted the array by year wise , but I need to sort by month descending .
Array
(
[0] => Array
(
[sales] => 117513.00000000
[month] => 1
[month_name] => January
[year] => 2018
)
[1] => Array
(
[purchases] => 6447.00000000
[month] => 12
[month_name] => December
[year] => 2017
)
[2] => Array
(
[sales] => 23025.00000000
[month] => 11
[month_name] => November
[year] => 2017
)
[3] => Array
(
[sales] => 136350.00000000
[month] => 9
[month_name] => September
[year] => 2017
)
)
How can I get the second format , which is sorted by month..
Use array_multisort function to do this.
<?php
// Obtain a list of columns
foreach ($data as $key => $row) {
$month[$key] = $row['month'];
}
array_multisort($month, SORT_DESC);
?>

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

unset all id keys from multidimensional array

How to unset all keys named id from a multidimensional array?
private function remove_id(Array &$arr){
foreach($arr as $key => $value){
if($key == 'id'){
unset($arr[$key]);
}
elseif(is_array($value)){
$this->remove_id($value);
}
}
}
array
Array
(
[id] => 52453
[periods] => Array
(
[0] => Array
(
[id] => 95296
[is_readonly] => 0
[year] => 2016
[month] => 1
)
[1] => Array
(
[id] => 95297
[is_readonly] => 0
[year] => 2016
[month] => 2
)
[2] => Array
(
[id] => 95298
[is_readonly] => 0
[year] => 2016
[month] => 3
)
[3] => Array
(
[id] => 95299
[is_readonly] => 0
[year] => 2016
[month] => 4
)
[4] => Array
(
[id] => 95300
[is_readonly] => 0
[year] => 2016
[month] => 5
)
[5] => Array
(
[id] => 95301
[is_readonly] => 0
[year] => 2016
[month] => 6
)
[6] => Array
(
[id] => 95302
[is_readonly] => 0
[year] => 2016
[month] => 7
)
[7] => Array
(
[id] => 95303
[is_readonly] => 0
[year] => 2016
[month] => 8
)
[8] => Array
(
[id] => 95304
[is_readonly] => 0
[year] => 2016
[month] => 9
)
[9] => Array
(
[id] => 95305
[is_readonly] => 0
[year] => 2016
[month] => 10
)
[10] => Array
(
[id] => 95306
[is_readonly] => 0
[year] => 2016
[month] => 11
)
[11] => Array
(
[id] => 95307
[is_readonly] => 0
[year] => 2016
[month] => 12
)
)
[closing_profit_amount] => 0
[closing_profit_net_amount] => 0
)
You need to use &value in the for loop to keep changes in nested arrays.
Also the array cannot have more than 1 id key, so there is no need to check it within the loop, which allow a bit of microoptimization here:
private function remove_id(Array &$arr){
if(isset($arr['id'])) {
unset($arr['id']);
}
foreach($arr as &$value){
if(is_array($value)){
$this->remove_id($value);
}
}
}

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