{"id":7031,"date":"2024-11-14T11:52:24","date_gmt":"2024-11-14T03:52:24","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7031\/"},"modified":"2024-11-14T11:52:24","modified_gmt":"2024-11-14T03:52:24","slug":"%e5%88%a9%e7%94%a8%e6%95%b0%e7%bb%84%e5%88%86%e7%bb%84%e5%92%8c%e5%bd%92%e5%b9%b6%e6%b1%82%e5%92%8c%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e9%94%ae%e9%87%8d%e5%8f%a0%e4%ba%8c%e7%bb%b4%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7031\/","title":{"rendered":"\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5982\u4f55\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173033586340906.jpg\" class=\"aligncenter\" title=\"\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5982\u4f55\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76\uff1f\u63d2\u56fe\" alt=\"\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5982\u4f55\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76<\/strong><\/p>\n<p>\u7ed9\u5b9a\u4e00\u4e2a\u5e26\u6709\u591a\u7ef4\u952e\u7684\u5173\u8054\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5176\u6309\u6307\u5b9a\u5206\u7ec4\u89c4\u5219\u5408\u5e76\uff0c\u5f62\u6210\u65b0\u7684\u952e\u91cd\u53e0\u6570\u7ec4\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u6307\u5b9a\u5206\u7ec4\u957f\u5ea6\u4e3a6\u65f6\uff0c\u539f\u6570\u7ec4\u4e2d\u7684\u952e\u503c\u9700\u6309\u5982\u4e0b\u89c4\u5219\u5206\u7ec4\u5e76\u5408\u5e76\uff1a<\/p>\n<ul>\n<li>\u5bf9\u4e8e\u5c0f\u4e8e6\u4e2a\u7684\u952e\uff0c\u76f4\u63a5\u4fdd\u5b58\u5728\u65b0\u6570\u7ec4\u4e2d\uff1b<\/li>\n<li>\u5bf9\u4e8e\u5927\u4e8e6\u4e2a\u7684\u952e\uff0c\u5c06\u5176\u63096\u4e2a\u4e00\u7ec4\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5206\u522b\u5408\u5e76\u6bcf\u4e2a\u7ec4\u5185\u7684\u952e\u503c\uff08\u6c42\u548c\uff09\uff0c\u4ee5\u201c\u5f00\u59cb\u65e5\u671f \/ \u7ed3\u675f\u65e5\u671f\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u952e\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6848<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u5229\u7528php\u4e2d\u7684array_chunk\u51fd\u6570\u5c06\u539f\u6570\u7ec4\u6309\u7167\u6307\u5b9a\u7684\u957f\u5ea6\u8fdb\u884c\u5206\u7ec4\uff0c\u518d\u7ed3\u5408array_reduce\u51fd\u6570\u5b9e\u73b0\u7ec4\u5185\u6570\u636e\u7684\u6c42\u548c\uff1a<\/p>\n<pre>$arr = [\n    'categories' =&gt; [\n        '22-01-30',\n        '22-01-31',\n        '22-02-01',\n        '22-02-02',\n        '22-02-03',\n        '22-02-04',\n        '22-02-05',\n        '22-02-06',\n        '22-02-07',\n        '22-02-08',\n        '22-02-09',\n        '22-02-10',\n        '22-02-11',\n        '22-02-12',\n        '22-02-13',\n        '22-02-14',\n        '22-02-15',\n        '22-02-16',\n        '22-02-17',\n        '22-02-18',\n        '22-02-19',\n        '22-02-20',\n        '22-02-21',\n        '22-02-22',\n        '22-02-23',\n        '22-02-24',\n        '22-02-25',\n        '22-02-26',\n        '22-02-27',\n        '22-02-28',\n    ],\n    'data' =&gt; [\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '0',\n        '5638.04',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '0.00',\n        '25915.99',\n        '1606.44',\n        '1676.90',\n        '0.00',\n        '0.00',\n        '1203.08',\n    ],\n];\n\n$chunk = 6;\n\n$result = array_reduce(array_chunk($arr['categories'], $chunk), function ($result, $current) use ($chunk, $arr) {\n    $start = current($current);\n    $end = end($current);\n    $result['categories'][] = $start . '\/' . $end;\n\n    $count = count($result['categories'] ?? []);\n    $result['data'][] = array_sum(array_slice($arr['data'] ?? [], $count * $chunk, $chunk));\n\n    return $result;\n}, ['categories' =&gt; [], 'data' =&gt; []]);\n\nvar_dump($result, array_sum($arr['data']) === array_sum($result['data']));<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff1a<\/p>\n<ul>\n<li>array_chunk\u51fd\u6570\u5c06$arr[&#8216;categories&#8217;]\u6570\u7ec4\u63096\u4e2a\u4e3a\u4e00\u7ec4\u8fdb\u884c\u5206\u7ec4\uff0c\u8fd4\u56de\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4\u3002<\/li>\n<li>\n<p>array_reduce\u51fd\u6570\u5bf9\u5206\u7ec4\u540e\u7684\u6570\u7ec4\u8fdb\u884c\u904d\u5386\uff0c\u5e76\u6309\u5982\u4e0b\u89c4\u5219\u6267\u884c\uff1a<\/p>\n<ul>\n<li>\u83b7\u53d6\u6bcf\u4e00\u7ec4\u7684\u7b2c\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u5f00\u59cb\u65e5\u671f\uff0c\u6700\u540e\u4e00\u4e2a\u5143\u7d20\u4f5c\u4e3a\u7ed3\u675f\u65e5\u671f\uff0c\u62fc\u63a5\u4e3a\u201c\u5f00\u59cb\u65e5\u671f\/\u7ed3\u675f\u65e5\u671f\u201d\u7684\u683c\u5f0f\uff0c\u4fdd\u5b58\u5230$result[&#8216;categories&#8217;]\u4e2d\u3002<\/li>\n<li>\u8ba1\u7b97\u5f53\u524d\u7ec4\u5728$arr[&#8216;categories&#8217;]\u4e2d\u7684\u4f4d\u7f6e\uff0c\u7528$count\u53d8\u91cf\u8868\u793a\u3002<\/li>\n<li>\u4ece$arr[&#8216;data&#8217;]\u4e2d\u622a\u53d6\u4ece$count * $chunk\u5f00\u59cb\u7684$chunk\u4e2a\u5143\u7d20\uff0c\u5c06\u5176\u6c42\u548c\u5e76\u4fdd\u5b58\u5230$result[&#8216;data&#8217;]\u4e2d\u3002<\/li>\n<\/ul>\n<\/li>\n<li>var_dump\u51fd\u6570\u540c\u65f6\u6253\u5370\u5408\u5e76\u540e\u7684$result\u6570\u7ec4\u548c\u539f\u59cb$arr\u6570\u636e\u4e4b\u548c\u662f\u5426\u76f8\u7b49\uff0c\u4ee5\u9a8c\u8bc1\u6570\u636e\u7684\u6b63\u786e\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5982\u4f55\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5229\u7528\u6570\u7ec4\u5206\u7ec4\u548c\u5f52\u5e76\u6c42\u548c\u5b9e\u73b0\u952e\u91cd\u53e0\u4e8c\u7ef4\u6570\u7ec4\u6570\u636e\u7684\u5408\u5e76 \u7ed9\u5b9a\u4e00\u4e2a\u5e26\u6709\u591a\u7ef4\u952e\u7684\u5173\u8054\u6570\u7ec4\uff0c\u9700\u8981\u5c06\u5176\u6309\u6307\u5b9a\u5206\u7ec4\u89c4\u5219\u5408\u5e76\uff0c\u5f62\u6210\u65b0\u7684\u952e\u91cd\u53e0\u6570\u7ec4\u3002\u5177\u4f53\u6765\u8bf4\uff0c\u5f53\u6307\u5b9a\u5206\u7ec4\u957f\u5ea6\u4e3a6\u65f6\uff0c\u539f\u6570\u7ec4\u4e2d\u7684\u952e\u503c\u9700\u6309\u5982\u4e0b\u89c4\u5219\u5206\u7ec4\u5e76\u5408\u5e76\uff1a \u5bf9\u4e8e\u5c0f\u4e8e6\u4e2a\u7684\u952e\uff0c\u76f4\u63a5\u4fdd\u5b58\u5728\u65b0\u6570\u7ec4\u4e2d\uff1b \u5bf9\u4e8e\u5927\u4e8e6\u4e2a\u7684\u952e\uff0c\u5c06\u5176\u63096\u4e2a\u4e00\u7ec4\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u5206\u522b\u5408\u5e76\u6bcf\u4e2a\u7ec4\u5185\u7684\u952e\u503c\uff08\u6c42\u548c\uff09\uff0c\u4ee5\u201c\u5f00\u59cb\u65e5\u671f \/ \u7ed3\u675f\u65e5\u671f\u201d\u7684\u5f62\u5f0f\u5b58\u50a8\u952e\u503c\u3002 \u89e3\u51b3\u65b9\u6848 \u6211\u4eec\u53ef\u4ee5\u5229\u7528php\u4e2d\u7684array_chunk\u51fd\u6570\u5c06\u539f\u6570\u7ec4\u6309\u7167\u6307\u5b9a\u7684\u957f\u5ea6\u8fdb\u884c\u5206\u7ec4\uff0c\u518d\u7ed3\u5408array_reduce\u51fd\u6570\u5b9e\u73b0\u7ec4\u5185\u6570\u636e\u7684\u6c42\u548c\uff1a $arr = [ &#8216;categories&#8217; =&gt; [ &#8217;22-01-30&#8242;, &#8217;22-01-31&#8242;, &#8217;22-02-01&#8242;, &#8217;22-02-02&#8242;, &#8217;22-02-03&#8242;, &#8217;22-02-04&#8242;, &#8217;22-02-05&#8242;, &#8217;22-02-06&#8242;, &#8217;22-02-07&#8242;, &#8217;22-02-08&#8242;, &#8217;22-02-09&#8242;, &#8217;22-02-10&#8242;, &#8217;22-02-11&#8242;, &#8217;22-02-12&#8242;, &#8217;22-02-13&#8242;, &#8217;22-02-14&#8242;, &#8217;22-02-15&#8242;, &#8217;22-02-16&#8242;, &#8217;22-02-17&#8242;, &#8217;22-02-18&#8242;, &#8217;22-02-19&#8242;, &#8217;22-02-20&#8242;, &#8217;22-02-21&#8242;, &#8217;22-02-22&#8242;, &#8217;22-02-23&#8242;, &#8217;22-02-24&#8242;, &#8217;22-02-25&#8242;, &#8217;22-02-26&#8242;, &#8217;22-02-27&#8242;, &#8217;22-02-28&#8242;, ], &#8216;data&#8217; =&gt; [ &#8216;0&#8217;, &#8216;0&#8217;, &#8216;0&#8217;, &#8216;0&#8217;, &#8216;0&#8217;, &#8216;0&#8217;, &#8216;0&#8217;, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7031","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7031","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7031"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7031\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7031"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7031"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7031"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}