{"id":16805,"date":"2022-10-22T05:45:46","date_gmt":"2022-10-22T00:15:46","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/"},"modified":"2022-10-22T05:45:46","modified_gmt":"2022-10-22T00:15:46","slug":"solved-php-average-by-group-of-n-elements-in-an-array","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/","title":{"rendered":"[Solved] PHP average by group of n elements in an array"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48399126\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48399126\" data-parentid=\"48398795\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>First of all, you should redesign your data structure. You aggregate the values in strings in <code>$values<\/code> and then it&#8217;s difficult to extract and use the individual components.<\/p>\n<pre><code>$values = array();\nfor($i=1;$i&lt;=$arrayCount;$i++){\n    $date = trim($allDataInSheet[$i][\"A\"]);\n    $dir = trim($allDataInSheet[$i][\"B\"]);\n    $sinvalue= sin(deg2rad($dir));\n    $cosvalue= cos(deg2rad($dir));            \n    $values[] = array(\n        'date' =&gt; $date,\n        'sin'  =&gt; $sinvalue,\n        'cos'  =&gt; $cosvalue,\n    );\n}\n<\/code><\/pre>\n<p>Now, several <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/ref.array.php\">array functions<\/a> can be applied to do the job.<\/p>\n<p>PHP does not provide a function to compute the average for a list of numbers but it is not difficult to write one:<\/p>\n<pre><code>function average(array $input)\n{\n    return array_sum($input) \/ count($input);\n}\n<\/code><\/pre>\n<p>Extract the values associated to <code>'sin'<\/code> in <code>$values<\/code>, split the list in chunks of 5 items, apply the function <code>average()<\/code> to each chunk. The result is a list of 288 values.<\/p>\n<pre><code>$avg288 = array_map(\n    'average',\n    array_chunk(\n        array_column($values, 'sin'),\n        5\n    )\n);\n<\/code><\/pre>\n<p>Similar to above, split the list of 288 averages into 24 chunks of 12 values, apply <code>average()<\/code> to each chunk, get a list of 24 numbers.<\/p>\n<pre><code>$avg24 = array_map(\n    'average',\n    array_chunk(\n        $avg288,\n        12\n    )\n);\n<\/code><\/pre>\n<p>Read about <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-sum.php\"><code>array_sum()<\/code><\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.count.php\"><code>count()<\/code><\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-column.php\"><code>array_column()<\/code><\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-chunk.php\"><code>array_chunk()<\/code><\/a>, <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-map.php\"><code>array_map()<\/code><\/a>.<\/p>\n<hr>\n<p><strong>Update<\/strong><\/p>\n<p>On PHP 5.4 <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-column.php\"><code>array_column()<\/code><\/a> doesn&#8217;t work (because it was introduced in PHP 5.5.).<\/p>\n<p>In this case you can compute the array returned by <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-column.php\"><code>array_column()<\/code><\/a> in the initial loop, where <code>$values<\/code> is created:<\/p>\n<pre><code>$values = array();\n$sins = array();\nfor($i=1;$i&lt;=$arrayCount;$i++){\n    $date = trim($allDataInSheet[$i][\"A\"]);\n    $dir = trim($allDataInSheet[$i][\"B\"]);\n    $sinvalue= sin(deg2rad($dir));\n    $cosvalue= cos(deg2rad($dir));            \n    $values[] = array(\n        'date' =&gt; $date,\n        'sin'  =&gt; $sinvalue,\n        'cos'  =&gt; $cosvalue,\n    );\n    $sins[] = $sinvalue;\n}\n<\/code><\/pre>\n<p>The use <code>$sins<\/code> instead of <code>array_column($values, 'sin')<\/code> in the computation of <code>$avg288<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved PHP average by group of n elements in an array <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First of all, you should redesign your data structure. You aggregate the values in strings in $values and then it&#8217;s difficult to extract and use the individual components. $values = array(); for($i=1;$i&lt;=$arrayCount;$i++){ $date = trim($allDataInSheet[$i][&#8220;A&#8221;]); $dir = trim($allDataInSheet[$i][&#8220;B&#8221;]); $sinvalue= sin(deg2rad($dir)); $cosvalue= cos(deg2rad($dir)); $values[] = array( &#8216;date&#8217; =&gt; $date, &#8216;sin&#8217; =&gt; $sinvalue, &#8216;cos&#8217; =&gt; $cosvalue, &#8230; <a title=\"[Solved] PHP average by group of n elements in an array\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/\" aria-label=\"More on [Solved] PHP average by group of n elements in an array\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[339],"class_list":["post-16805","post","type-post","status-publish","format-standard","hentry","category-solved","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] PHP average by group of n elements in an array - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] PHP average by group of n elements in an array - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] First of all, you should redesign your data structure. You aggregate the values in strings in $values and then it&#8217;s difficult to extract and use the individual components. $values = array(); for($i=1;$i&lt;=$arrayCount;$i++){ $date = trim($allDataInSheet[$i][&quot;A&quot;]); $dir = trim($allDataInSheet[$i][&quot;B&quot;]); $sinvalue= sin(deg2rad($dir)); $cosvalue= cos(deg2rad($dir)); $values[] = array( &#039;date&#039; =&gt; $date, &#039;sin&#039; =&gt; $sinvalue, &#039;cos&#039; =&gt; $cosvalue, ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-22T00:15:46+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] PHP average by group of n elements in an array\",\"datePublished\":\"2022-10-22T00:15:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/\"},\"wordCount\":172,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/\",\"name\":\"[Solved] PHP average by group of n elements in an array - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-10-22T00:15:46+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-php-average-by-group-of-n-elements-in-an-array\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] PHP average by group of n elements in an array\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] PHP average by group of n elements in an array - JassWeb","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":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] PHP average by group of n elements in an array - JassWeb","og_description":"[ad_1] First of all, you should redesign your data structure. You aggregate the values in strings in $values and then it&#8217;s difficult to extract and use the individual components. $values = array(); for($i=1;$i&lt;=$arrayCount;$i++){ $date = trim($allDataInSheet[$i][\"A\"]); $dir = trim($allDataInSheet[$i][\"B\"]); $sinvalue= sin(deg2rad($dir)); $cosvalue= cos(deg2rad($dir)); $values[] = array( 'date' =&gt; $date, 'sin' =&gt; $sinvalue, 'cos' =&gt; $cosvalue, ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/","og_site_name":"JassWeb","article_published_time":"2022-10-22T00:15:46+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] PHP average by group of n elements in an array","datePublished":"2022-10-22T00:15:46+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/"},"wordCount":172,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/","url":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/","name":"[Solved] PHP average by group of n elements in an array - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-22T00:15:46+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-php-average-by-group-of-n-elements-in-an-array\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] PHP average by group of n elements in an array"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=16805"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/16805\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=16805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=16805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=16805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}