{"id":13170,"date":"2023-06-02T23:24:20","date_gmt":"2023-06-03T03:24:20","guid":{"rendered":"http:\/\/local.brightwhiz\/?p=13170"},"modified":"2023-06-02T23:24:24","modified_gmt":"2023-06-03T03:24:24","slug":"calculate-the-rolling-average-in-php","status":"publish","type":"post","link":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/","title":{"rendered":"How to Calculate the Rolling Average of Numbers in PHP"},"content":{"rendered":"\n

Before we get to show you how to calculate the rolling average of an array or series of numbers it is important to understand what it is.<\/p>\n\n\n\n

What is a Rolling Average?<\/h2>\n\n\n\n

A rolling average, also known as a moving average, is a statistical calculation used to analyze data over a specific time period. It smooths out short-term fluctuations and highlights longer-term trends or patterns in the data.<\/p>\n\n\n\n

To calculate a rolling average, you select a window or interval of time (e.g., days, weeks, months) and compute the average value of the data points within that window. As you move forward in time, the window “rolls” or shifts, including new data points and excluding older ones. This process continues until you have computed the rolling average for all the available data points.<\/p>\n\n\n\n

The rolling average is often used to analyze time series data, such as stock prices, sales figures, or temperature readings. It helps to eliminate noise or volatility in the data, making it easier to identify underlying trends or patterns. By using a rolling average, you can obtain a smoother representation of the data, which can aid in making predictions or detecting long-term changes.<\/p>\n\n\n\n

Different types of rolling averages exist, including the simple moving average (SMA), exponential moving average (EMA), and weighted moving average (WMA). Each type has its own calculation method and is suited for different applications.<\/p>\n\n\n\n

Calculate the Rolling Average in PHP<\/h2>\n\n\n\n

Here we will use PHP<\/a> as an example to calculate a rolling average of an array of numbers you can use a loop to iterate through the array of values and calculate the average for each window of elements. Here’s an example code snippet that demonstrates the process:<\/p>\n\n\n\n

function calculateRollingAverage($numbers, $windowSize) {\n    $rollingAverage = [];\n    $sum = 0;\n\n    for ($i = 0; $i < count($numbers); $i++) {\n        $sum += $numbers[$i];\n\n        if ($i >= $windowSize) {\n            $sum -= $numbers[$i - $windowSize];\n            $rollingAverage[] = $sum \/ $windowSize;\n        } elseif ($i + 1 >= $windowSize) {\n            $rollingAverage[] = $sum \/ ($i + 1);\n        }\n    }\n\n    return $rollingAverage;\n}\n\n\/\/ Example usage\n$numbers = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20];\n$windowSize = 3;\n\n$rollingAvg = calculateRollingAverage($numbers, $windowSize);\nprint_r($rollingAvg);<\/code><\/pre>\n\n\n\n

In this example, the calculateRollingAverage<\/code> function takes two parameters: $numbers<\/code> (the array of numbers) and $windowSize<\/code> (the size of the rolling window). It initializes an empty array $rollingAverage<\/code> to store the calculated rolling averages.<\/p>\n\n\n\n

The function then iterates through the $numbers<\/code> array using a for loop. It keeps track of the sum of the current window of elements using the variable $sum<\/code>. At each iteration, it adds the current element to $sum<\/code> and checks if the window size has been reached.<\/p>\n\n\n\n

If the window size has been reached or exceeded, it subtracts the first element of the previous window from $sum<\/code> and calculates the average by dividing $sum by the window size. The calculated average is then added to the $rollingAverage<\/code> array.<\/p>\n\n\n\n

Finally, the function returns the $rollingAverage<\/code> array containing the calculated rolling averages.<\/p>\n\n\n\n

In the example usage section, an array [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] is provided, and a window size of 3 is specified. The calculateRollingAverage<\/code> function is called with these parameters, and the resulting rolling averages are printed using print_r<\/code>.<\/p>\n\n\n\n

The output of the example would be:<\/p>\n\n\n\n

Array\n(\n    [0] => 4\n    [1] => 6\n    [2] => 8\n    [3] => 10\n    [4] => 12\n    [5] => 14\n    [6] => 16\n    [7] => 18\n)<\/code><\/pre>\n\n\n\n

These values represent the rolling averages for each window of size 3 in the original array. The above formula can be applied to any other programming language.<\/p>\n","protected":false},"excerpt":{"rendered":"

Before we get to show you how to calculate the rolling average of an array or series of numbers it is important to understand what it is. What is a…<\/p>\n","protected":false},"author":1,"featured_media":13171,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,23,9,27,16],"tags":[433,452,471,544,545],"yoast_head":"\nHow can I Calculate the Rolling Average of Numbers in PHP<\/title>\n<meta name=\"description\" content=\"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How can I Calculate the Rolling Average of Numbers in PHP\" \/>\n<meta property=\"og:description\" content=\"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements\" \/>\n<meta property=\"og:url\" content=\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Brightwhiz.com\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/brightwhiz\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-03T03:24:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-06-03T03:24:24+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Michael Bright\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:site\" content=\"@brightwhizmag\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Michael Bright\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#article\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\"},\"author\":{\"name\":\"Michael Bright\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\"},\"headline\":\"How to Calculate the Rolling Average of Numbers in PHP\",\"datePublished\":\"2023-06-03T03:24:20+00:00\",\"dateModified\":\"2023-06-03T03:24:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\"},\"wordCount\":483,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg\",\"keywords\":[\"Optimization\",\"PHP\",\"Programming\",\"Software development\",\"Software Engineering\"],\"articleSection\":[\"Articles\",\"Guides\",\"How To\",\"Programming\",\"Technology\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\",\"url\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\",\"name\":\"How can I Calculate the Rolling Average of Numbers in PHP\",\"isPartOf\":{\"@id\":\"http:\/\/local.brightwhiz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg\",\"datePublished\":\"2023-06-03T03:24:20+00:00\",\"dateModified\":\"2023-06-03T03:24:24+00:00\",\"description\":\"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements\",\"breadcrumb\":{\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg\",\"width\":1280,\"height\":680,\"caption\":\"Calculate the Rolling Average\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"http:\/\/local.brightwhiz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Calculate the Rolling Average of Numbers in PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"http:\/\/local.brightwhiz\/#website\",\"url\":\"http:\/\/local.brightwhiz\/\",\"name\":\"Brightwhiz.com\",\"description\":\"Best Tech guides, Tutorials, and News\",\"publisher\":{\"@id\":\"http:\/\/local.brightwhiz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/local.brightwhiz\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"http:\/\/local.brightwhiz\/#organization\",\"name\":\"Brightwhiz\",\"url\":\"http:\/\/local.brightwhiz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"contentUrl\":\"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png\",\"width\":706,\"height\":135,\"caption\":\"Brightwhiz\"},\"image\":{\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/brightwhiz\/\",\"https:\/\/x.com\/brightwhizmag\",\"https:\/\/instagram.com\/bright_whiz\/\",\"https:\/\/www.pinterest.com\/sobbayi\/\",\"https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ\"]},{\"@type\":\"Person\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32\",\"name\":\"Michael Bright\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/\",\"url\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"contentUrl\":\"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g\",\"caption\":\"Michael Bright\"},\"sameAs\":[\"https:\/\/sobbayi.com\"],\"url\":\"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How can I Calculate the Rolling Average of Numbers in PHP","description":"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/","og_locale":"en_US","og_type":"article","og_title":"How can I Calculate the Rolling Average of Numbers in PHP","og_description":"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements","og_url":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/","og_site_name":"Brightwhiz.com","article_publisher":"https:\/\/www.facebook.com\/brightwhiz\/","article_published_time":"2023-06-03T03:24:20+00:00","article_modified_time":"2023-06-03T03:24:24+00:00","og_image":[{"width":1280,"height":680,"url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg","type":"image\/jpeg"}],"author":"Michael Bright","twitter_card":"summary_large_image","twitter_creator":"@brightwhizmag","twitter_site":"@brightwhizmag","twitter_misc":{"Written by":"Michael Bright","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#article","isPartOf":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/"},"author":{"name":"Michael Bright","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32"},"headline":"How to Calculate the Rolling Average of Numbers in PHP","datePublished":"2023-06-03T03:24:20+00:00","dateModified":"2023-06-03T03:24:24+00:00","mainEntityOfPage":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/"},"wordCount":483,"commentCount":0,"publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"image":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg","keywords":["Optimization","PHP","Programming","Software development","Software Engineering"],"articleSection":["Articles","Guides","How To","Programming","Technology"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#respond"]}]},{"@type":"WebPage","@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/","url":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/","name":"How can I Calculate the Rolling Average of Numbers in PHP","isPartOf":{"@id":"http:\/\/local.brightwhiz\/#website"},"primaryImageOfPage":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage"},"image":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage"},"thumbnailUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg","datePublished":"2023-06-03T03:24:20+00:00","dateModified":"2023-06-03T03:24:24+00:00","description":"To calculate a rolling average you can use a loop to iterate through the array of values and calculate the average for each window of elements","breadcrumb":{"@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#primaryimage","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2023\/06\/Calculate-the-Rolling-Average.jpg","width":1280,"height":680,"caption":"Calculate the Rolling Average"},{"@type":"BreadcrumbList","@id":"http:\/\/local.brightwhiz\/calculate-the-rolling-average-in-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"http:\/\/local.brightwhiz\/"},{"@type":"ListItem","position":2,"name":"How to Calculate the Rolling Average of Numbers in PHP"}]},{"@type":"WebSite","@id":"http:\/\/local.brightwhiz\/#website","url":"http:\/\/local.brightwhiz\/","name":"Brightwhiz.com","description":"Best Tech guides, Tutorials, and News","publisher":{"@id":"http:\/\/local.brightwhiz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/local.brightwhiz\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"http:\/\/local.brightwhiz\/#organization","name":"Brightwhiz","url":"http:\/\/local.brightwhiz\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/","url":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","contentUrl":"http:\/\/local.brightwhiz\/wp-content\/uploads\/2021\/11\/brightwhiz-com-logo-orange.png","width":706,"height":135,"caption":"Brightwhiz"},"image":{"@id":"http:\/\/local.brightwhiz\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/brightwhiz\/","https:\/\/x.com\/brightwhizmag","https:\/\/instagram.com\/bright_whiz\/","https:\/\/www.pinterest.com\/sobbayi\/","https:\/\/www.youtube.com\/channel\/UC6sCdP_d_RiTIM7ErFT-PSQ"]},{"@type":"Person","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/81f0f3126f13834ae2e7f381b3028e32","name":"Michael Bright","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"http:\/\/local.brightwhiz\/#\/schema\/person\/image\/","url":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","contentUrl":"http:\/\/1.gravatar.com\/avatar\/da90485875ff0aafa38fdd494abe87d1?s=96&d=mm&r=g","caption":"Michael Bright"},"sameAs":["https:\/\/sobbayi.com"],"url":"http:\/\/local.brightwhiz\/author\/sobbayiadmin\/"}]}},"_links":{"self":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/13170"}],"collection":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/comments?post=13170"}],"version-history":[{"count":0,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/posts\/13170\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media\/13171"}],"wp:attachment":[{"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/media?parent=13170"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/categories?post=13170"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/local.brightwhiz\/wp-json\/wp\/v2\/tags?post=13170"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}