Based on PHP sum array value based on textual duplicate in another array I have a question if I change topic to PHP average array value based on textual duplicate in another array.
I need to average array value based on textual duplicate in another array from that link.
Example:
$dateclosed = array("28-09-2024", "30-09-2024", "30-09-2024", "30-09-2024", "16-09-2024");
$medpo = array(23.83, 17.27, 40.55, 34.11, 24.94);
So the result looks like this:
$dateclosed = array("28-09-2024", "30-09-2024", "16-09-2024");
$medpo = array(23.83, 30.64333333333333, 24.94);
from above
23.83 from 23.83/1
24.94 from 24.94/1
30.64333333333333 from (17.27+40.55+34.11)/3
From the linked example I tried this code:
$new_array_calc = array();
foreach($dateclosed as $key => $value) {
$new_array_calc[$value] = ($new_array_calc[$value] ?? 0) + $medpo[$key];
$new_array_calc[$value] = $new_array_calc[$value] / count($new_array_calc);
}
print_r($new_array_calc);
It shows:
Array ( [28-09-2024] => 23.83 [30-09-2024] => 29.35125 [16-09-2024] => 8.3133333333333 )
I don't know how to find exactly count of each date such as 23.83 (count one) 91.93 (count three) 24.94 (count one) and calculate in one line.
Based on PHP sum array value based on textual duplicate in another array I have a question if I change topic to PHP average array value based on textual duplicate in another array.
I need to average array value based on textual duplicate in another array from that link.
Example:
$dateclosed = array("28-09-2024", "30-09-2024", "30-09-2024", "30-09-2024", "16-09-2024");
$medpo = array(23.83, 17.27, 40.55, 34.11, 24.94);
So the result looks like this:
$dateclosed = array("28-09-2024", "30-09-2024", "16-09-2024");
$medpo = array(23.83, 30.64333333333333, 24.94);
from above
23.83 from 23.83/1
24.94 from 24.94/1
30.64333333333333 from (17.27+40.55+34.11)/3
From the linked example I tried this code:
$new_array_calc = array();
foreach($dateclosed as $key => $value) {
$new_array_calc[$value] = ($new_array_calc[$value] ?? 0) + $medpo[$key];
$new_array_calc[$value] = $new_array_calc[$value] / count($new_array_calc);
}
print_r($new_array_calc);
It shows:
Array ( [28-09-2024] => 23.83 [30-09-2024] => 29.35125 [16-09-2024] => 8.3133333333333 )
I don't know how to find exactly count of each date such as 23.83 (count one) 91.93 (count three) 24.94 (count one) and calculate in one line.
Share Improve this question edited Dec 31, 2024 at 23:58 mkrieger1 23.4k7 gold badges64 silver badges81 bronze badges asked Nov 20, 2024 at 6:16 jokalalilojokalalilo 11 bronze badge 2- Well, have you tried anything? You already know what maths is required...what is preventing you from implementing it? (Please bear in mind that "I need" isn't a question and we're not a free write-my-code service. We can help you with your attempt to implement your requirement. If you don't even know where to start, please explain what exactly is blocking you.) – ADyson Commented Nov 20, 2024 at 7:22
- from link eaxmple i tried code $new_array_calc = array(); foreach($dateclosed as $key => $value){ $new_array_calc[$value] = ($new_array_calc[$value] ?? 0) + $medpo[$key]; $new_array_calc[$value] = $new_array_calc[$value] / count($new_array_calc); } print_r($new_array_calc); its show Array ( [28-09-2024] => 23.83 [30-09-2024] => 29.35125 [16-09-2024] => 8.3133333333333 ) i dont know what to find exactly count array such as 23.83(count one) 91.93(count three) 24.94(count one) and calculate in one line sorry for my english skill – jokalalilo Commented Nov 20, 2024 at 8:18
2 Answers
Reset to default 0A simple way to do this is to apply the average calculation in a separate loop after you have populated the new array. That way, you've already got the final total value that you need to divide by. Doing it inside the foreach($dateclosed...
loop can't really work, because you don't know if you've found the last entry for any given date yet, or not.
The array_count_values()
function will also help you here - it's a quick way to tell you how many entries occur for each date in the original $dateClosed
array - which you need for the average calculation.
Here's a working example:
$dateclosed = array("28-09-2024", "30-09-2024", "30-09-2024", "30-09-2024", "16-09-2024");
$medpo = array(23.83, 17.27, 40.55, 34.11, 24.94);
$new_array_calc = array();
$dateCounts = array_count_values($dateclosed);
foreach ($dateclosed as $key => $value)
{
$new_array_calc[$value] = ($new_array_calc[$value] ?? 0) + $medpo[$key];
}
//calculate the averages
foreach ($new_array_calc as $key => &$value)
{
$value = $value / $dateCounts[$key];
}
print_r($new_array_calc);
This outputs:
Array
(
[28-09-2024] => 23.83
[30-09-2024] => 30.643333333333
[16-09-2024] => 24.94
)
Live demo: https://3v4l./c0QJ5
To correctly calculate average values based on textual duplicates, you need to keep track of both sum and count of each date. Here's another way, best for large data sets:
<?php
$dateclosed = array('28-09-2024', '30-09-2024', '30-09-2024', '30-09-2024', '16-09-2024');
$medpo = array(23.83, 17.27, 40.55, 34.11, 24.94);
// Keeps total sum of values for each date.
$sum_array = array();
// Keeps count of occurrences for each date.
$count_array = array();
// Calculate sum and count for each date.
foreach($dateclosed as $key => $value) {
if (!isset($sum_array[$value])) {
$sum_array[$value] = 0;
$count_array[$value] = 0;
}
$sum_array[$value] += $medpo[$key];
$count_array[$value]++;
}
// Calculate average for each date.
$average_array = array();
foreach($sum_array as $key => $value) {
$average_array[$key] = $value / $count_array[$key];
}
print_r($average_array);
Result:
Array
(
[28-09-2024] => 23.83
[30-09-2024] => 30.643333333333
[16-09-2024] => 24.94
)