I have an array wherein I need to return the average of the "price" & "perUnitVolume" and the sum of the "qty" WHERE the Year & Month match. For example in this array, 2 elements in the array have Year = 2014 & Month = 11, these elements need to be summed and averaged. This is a simplified output of my array (actual array has > 1000 elements in the array, many variations of Year and Month), just don't know how to get the results that I need using php.
Array
(
[0] => Array
(
[asin] => XXXXXX
[price] => 19.99
[qtyTotal] => 22
[perUnitVolume] => 0.22
[Month] => 2014
[Year] => 11
[Day] => 14
)
[1] => Array
(
[asin] => YYYYYYY
[price] => 24.99
[qtyTotal] => 53
[perUnitVolume] => 0.54
[Month] => 2014
[Year] => 11
[Day] => 15
)
[2] => Array
(
[asin] => XXXXXX
[price] => 19.99
[qtyTotal] => 13
[perUnitVolume] => 0.99
[Month] => 2015
[Year] => 08
[Day] => 16
)
)
Basically in the end I need to end up with this:
Array
(
[0] => Array
(
[asin] => XXXXXX
[price] => 22.49 (average of 19.99 + 24.99 / 2)
[qtyTotal] => 75 (sum of 22 + 53)
[perUnitVolume] => 0.38 (average of 0.22 + 0.54 / 2)
[Month] => 2014
[Year] => 11
)
[1] => Array
(
[asin] => YYYYYYY
[price] => 19.99
[qtyTotal] => 53
[perUnitVolume] => 0.54
[Month] => 2015
[Year] => 08
)
)
Related
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
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
I have a multidimensional array like this one ( each $orders[$userid] array has multiple arrays of orders)
foreach($orders[$userid] as $order){
print_r($order);
}
This is the first user's orders grouped by date,month,year
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
As you see older dated are displayed after recent dates. The question is how to sort this multidimensional array to display old dates then recent dates like this
Array ( [id] => 405860 [user_id] => 26017 [total] => 10 [final_total] => 9.8 [order_status_id] => 1
[day] => 30 [month] => 10 [year] => 2016 [amount2] => 352.5999994277954 )
Array ( [id] => 407500 [user_id] => 26017 [total] => 85 [final_total] => 84.5 [order_status_id] => 1
[day] => 31 [month] => 10 [year] => 2016 [amount2] => 1135.1000022888184 )
Array ( [id] => 409079 [user_id] => 26017 [total] => 30 [final_total] => 29.1 [order_status_id] => 1
[day] => 1 [month] => 11 [year] => 2016 [amount2] => 2198.2999696731567 )
Array ( [id] => 410744 [user_id] => 26017 [total] => 175 [final_total] => 165 [order_status_id] => 1
[day] => 2 [month] => 11 [year] => 2016 [amount2] => 2619.799982070923 )
Array ( [id] => 412268 [user_id] => 26017 [total] => 300 [final_total] => 293 [order_status_id] => 1
[day] => 3 [month] => 11 [year] => 2016 [amount2] => 4413.400000572205 )
Please use code:
foreach ($orders as $key => $row) {
$year[$key] = $row['year'];
$month[$key] = $row['month'];
$day[$key] = $row['day'];
}
array_multisort($year, SORT_ASC, $month, SORT_ASC, $day, SORT_ASC, $orders);
While in more complex situations you would use array_multisort, in this case, you can reduce the problem to sort only by one value. The
'year', 'month' and 'day' are basically parts of the date. Using usort function you can do the following:
$dateToTimestamp = function (array $order) {
$day = $order['day'];
$month = $order['month'];
$year = $order['year'];
return strtotime("$day-$month-$year");
};
usort($array, function ($a, $b) use ($dateToTimestamp) {
return $dateToTimestamp($a) - $dateToTimestamp($b);
});
One thing to pay attention with strtotime you cannot really set a locale in order to recognize which part of the string is a day, month, or year. In one of PHP manual comments, there is a trick to compensate this. In our case, the use of dash separator ensures the order.
Here is working demo.
I have to filter this array to see only future items.
How do I unset items from which the timeEnd has expired?
i.e when I call this array at 17:00 there's only array[2] left.
Array
(
[0] => Array
(
[id] => 3034
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 09:30
[description] => User_A
[locationId] => 1
[roomId] => 8
[relationId] => 104
)
[1] => Array
(
[id] => 2524
[date] => 28-09-2016
[timeStart] => 08:30
[timeEnd] => 12:00
[description] => User_B
[locationId] => 1
[roomId] => 5
[relationId] => 86
)
[2] => Array
(
[id] => 2533
[date] => 28-09-2016
[timeStart] => 09:00
[timeEnd] => 18:00
[description] => User_C
[locationId] => 1
[roomId] => 4
[relationId] => 31
)
)
foreach ($reservations as $key=>$reservation) {
$expireDate = $reservation['date'].' '.$reservation['timeEnd'];
if (strtotime($expireDate) <= strtotime('now')){
unset($reservations[$key]);
}
}
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];
}
}
}