php convert same ids in multidimensional array to count from 0 - php

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

Related

Is it possible to manage the structure of returning array with QueryBuilder?

I'm trying to optimize my app. I need to know if what I wan to do is possible or not.
I made some try based on indexBy whitout success.
Currentely I am able to change the structure of the results array like I need via PHP.
I wan to know if QueryBuilder is able to make that for me and how (if possible).
Here is the query building :
return $this->createQueryBuilder('a')
->select('SUBSTRING(a.creationDate, 1,4) year, TRIM(LEADING \'0\' FROM SUBSTRING(a.creationDate, 6,2)) month, COUNT(a) number')
->where('a.creationDate > :limitDate')
->groupBy('year')
->addGroupBy('month')
->orderBy('year', 'ASC')
->addOrderBy('month', 'ASC')
->setParameter('limitDate', new \DateTime("2000-01-01 00:00:00") )
->getQuery()
->getArrayResult();
Here is the actual result of the query :
Array (
[0] => Array ( [year] => 2016 [month] => 10 [number] => 96 )
[1] => Array ( [year] => 2016 [month] => 11 [number] => 159 )
[2] => Array ( [year] => 2016 [month] => 12 [number] => 118 )
[3] => Array ( [year] => 2016 [month] => 9 [number] => 47 )
[4] => Array ( [year] => 2017 [month] => 1 [number] => 44 )
[5] => Array ( [year] => 2017 [month] => 10 [number] => 47 )
...
)
Here is PHP code I use to change the array like I wan :
$myRestructuredArray = [];
foreach($myArray as $line)
{
$myRestructuredArray [$line['year']][$line['month']] = $line['number'];
}
Here is the result I need :
Array (
[2016] => Array (
[10] => 96
[11] => 159
[12] => 118
[9] => 47 )
[2017] => Array (
[1] => 44
[10] => 47
...
)

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;
}

Add up multidimensional Sub Array Values by other Sub Array Values

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.

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

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