{"id":1111,"date":"2021-04-13T10:17:17","date_gmt":"2021-04-13T10:17:17","guid":{"rendered":"https:\/\/phptutorial.net\/?page_id=1111"},"modified":"2025-04-06T07:54:23","modified_gmt":"2025-04-06T07:54:23","slug":"php-array_reduce","status":"publish","type":"page","link":"https:\/\/www.phptutorial.net\/php-tutorial\/php-array_reduce\/","title":{"rendered":"PHP array_reduce Function"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the PHP <code>array_reduce()<\/code> function to reduce an array to a single value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-php-array_reduce-function'>Introduction to the PHP array_reduce function <a href=\"#introduction-to-the-php-array_reduce-function\" class=\"anchor\" id=\"introduction-to-the-php-array_reduce-function\" title=\"Anchor for Introduction to the PHP array_reduce function\">#<\/a><\/h2>\n\n\n\n<p>The <code>array_reduce()<\/code> function reduces an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-array\/\">array<\/a> to a single value using a <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-anonymous-functions\/\">callback function<\/a>. It&#8217;s easier to understand the <code>array_reduce()<\/code> function by example.<\/p>\n\n\n\n<p>The following example calculates the sum of all numbers in an array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">30<\/span>];\n\n$total = <span class=\"hljs-number\">0<\/span>;\n\n<span class=\"hljs-keyword\">foreach<\/span> ($numbers <span class=\"hljs-keyword\">as<\/span> $number) {\n    $total += $number;\n}\n\n<span class=\"hljs-keyword\">echo<\/span> $total; <span class=\"hljs-comment\">\/\/ 60<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzEwLDIwLDMwXTsKCiR0b3RhbCA9IDA7Cgpmb3JlYWNoICgkbnVtYmVycyBhcyAkbnVtYmVyKSB7CiAgICAkdG90YWwgKz0gJG51bWJlcjsKfQoKZWNobyAkdG90YWw7IC8vIDYw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, define the <code>$numbers<\/code> array that has three numbers 10, 20, and 30.<\/li>\n\n\n\n<li>Second, define a variable <code>$total<\/code> and initialize it to zero.<\/li>\n\n\n\n<li>Third, add up the numbers from the <code>$numbers<\/code> array to the <code>$total<\/code> variable iteratively using a <code><a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-foreach\/\">foreach<\/a><\/code> loop.<\/li>\n\n\n\n<li>Finally, show the value of the <code>$total<\/code> variable.<\/li>\n<\/ul>\n\n\n\n<p>Alternatively, you can use the <code>array_reduce()<\/code> function to achieve the same result without using the <code>foreach<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">30<\/span>];\n\n$total  = array_reduce($numbers, <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($previous, $current)<\/span> <\/span>{\n    <span class=\"hljs-keyword\">return<\/span> $previous + $current;\n});\n\n<span class=\"hljs-keyword\">echo<\/span> $total; <span class=\"hljs-comment\">\/\/ 60<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzEwLDIwLDMwXTsKCiR0b3RhbCAgPSBhcnJheV9yZWR1Y2UoJG51bWJlcnMsIGZ1bmN0aW9uICgkcHJldmlvdXMsICRjdXJyZW50KSB7CiAgICByZXR1cm4gJHByZXZpb3VzICsgJGN1cnJlbnQ7Cn0pOwoKZWNobyAkdG90YWw7IC8vIDYw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The <code>array_reduce()<\/code> function accepts an array and a callback function. It reduces the <code>$numbers<\/code> array to a single value using the callback function.<\/p>\n\n\n\n<p>Since PHP 7.3, you can use an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-arrow-functions\/\">arrow function<\/a> rather than an <a href=\"https:\/\/phptutorial.net\/php-tutorial\/php-anonymous-functions\/\">anonymous function<\/a> as the callback function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$numbers = &#91;<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">20<\/span>,<span class=\"hljs-number\">30<\/span>];\n\n$total  = array_reduce(\n    $numbers,\n    fn ($previous, $current) =&gt; $previous + $current\n);\n\n<span class=\"hljs-keyword\">echo<\/span> $total; <span class=\"hljs-comment\">\/\/ 60<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRudW1iZXJzID0gWzEwLDIwLDMwXTsKCiR0b3RhbCAgPSBhcnJheV9yZWR1Y2UoCiAgICAkbnVtYmVycywKICAgIGZuICgkcHJldmlvdXMsICRjdXJyZW50KSA9PiAkcHJldmlvdXMgKyAkY3VycmVudAopOwoKZWNobyAkdG90YWw7IC8vIDYw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-array_reduce-function-syntax'>PHP array_reduce() function syntax <a href=\"#php-array_reduce-function-syntax\" class=\"anchor\" id=\"php-array_reduce-function-syntax\" title=\"Anchor for PHP array_reduce() function syntax\">#<\/a><\/h2>\n\n\n\n<p>The following shows the <code>array_reduce()<\/code> function&#8217;s syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">array_reduce ( <span class=\"hljs-keyword\">array<\/span> $array , callable $callback , mixed $initial = <span class=\"hljs-keyword\">null<\/span> ) : mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>array_reduce()<\/code> function has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>$array<\/code> is the input array that will be reduced to a single value.<\/li>\n\n\n\n<li><code>$callback<\/code> is a callback function that determines how the array should be reduced.<\/li>\n\n\n\n<li><code>$initial<\/code> is a value that the <code>arrary_reduce()<\/code> function uses at the beginning of the reducing process. The array_reduce() function returns <code>$initial<\/code> as the final result if the <code>$array<\/code> is empty.<\/li>\n<\/ul>\n\n\n\n<p>If the input array is empty or the <code>$initial<\/code> is omitted, the <code>array_reduce()<\/code> function returns null.<\/p>\n\n\n\n<p>The <code>$callback<\/code> function is often called a <strong>reducer<\/strong>. It&#8217;s where you decide the logic for reducing the array elements. The callback function has the following signature:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">callback ( mixed $carry , mixed $item ) : mixed<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The callback function accepts two arguments:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The <code>$carry<\/code> holds the return value of the previous iteration. In the first iteration, it holds the value of the <code>$initial<\/code> instead.<\/li>\n\n\n\n<li>The <code>$item<\/code> holds the value of the current iteration.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='php-array_reduce-function-example'>PHP array_reduce function example <a href=\"#php-array_reduce-function-example\" class=\"anchor\" id=\"php-array_reduce-function-example\" title=\"Anchor for PHP array_reduce function example\">#<\/a><\/h2>\n\n\n\n<p>The following example uses the <code>array_reduce()<\/code> function to calculate the total items in a shopping cart:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$carts = &#91;\n    &#91;<span class=\"hljs-string\">'item'<\/span> =&gt; <span class=\"hljs-string\">'A'<\/span>, <span class=\"hljs-string\">'qty'<\/span> =&gt; <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-string\">'price'<\/span> =&gt; <span class=\"hljs-number\">10<\/span>],\n    &#91;<span class=\"hljs-string\">'item'<\/span> =&gt; <span class=\"hljs-string\">'B'<\/span>, <span class=\"hljs-string\">'qty'<\/span> =&gt; <span class=\"hljs-number\">3<\/span>, <span class=\"hljs-string\">'price'<\/span> =&gt; <span class=\"hljs-number\">20<\/span>],\n    &#91;<span class=\"hljs-string\">'item'<\/span> =&gt; <span class=\"hljs-string\">'C'<\/span>, <span class=\"hljs-string\">'qty'<\/span> =&gt; <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-string\">'price'<\/span> =&gt; <span class=\"hljs-number\">30<\/span>]\n];\n\n\n$total = array_reduce(\n    $carts,\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($prev, $item)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> $prev + $item&#91;<span class=\"hljs-string\">'qty'<\/span>] * $item&#91;<span class=\"hljs-string\">'price'<\/span>];\n    }\n);\n\n<span class=\"hljs-keyword\">echo<\/span> $total; <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjYXJ0cyA9IFsKICAgIFsnaXRlbScgPT4gJ0EnLCAncXR5JyA9PiAyLCAncHJpY2UnID0-IDEwXSwKICAgIFsnaXRlbScgPT4gJ0InLCAncXR5JyA9PiAzLCAncHJpY2UnID0-IDIwXSwKICAgIFsnaXRlbScgPT4gJ0MnLCAncXR5JyA9PiA1LCAncHJpY2UnID0-IDMwXQpdOwoKCiR0b3RhbCA9IGFycmF5X3JlZHVjZSgKICAgICRjYXJ0cywKICAgIGZ1bmN0aW9uICgkcHJldiwgJGl0ZW0pIHsKICAgICAgICByZXR1cm4gJHByZXYgKyAkaXRlbVsncXR5J10gKiAkaXRlbVsncHJpY2UnXTsKICAgIH0KKTsKCmVjaG8gJHRvdGFsOyAvLyAxNTU\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">230<\/code><\/span><\/pre>\n\n\n<p>If the <code>carts<\/code> array is empty, you&#8217;ll get the total as <code>null<\/code>.<\/p>\n\n\n\n<p>To return zero instead of <code>null<\/code>, you pass the initial argument as zero to the <code>array_reduce()<\/code> function like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-meta\">&lt;?php<\/span>\n\n$carts = &#91;];\n\n$total = array_reduce(\n    $carts,\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-params\">($prev, $item)<\/span> <\/span>{\n        <span class=\"hljs-keyword\">return<\/span> $prev + $item&#91;<span class=\"hljs-string\">'qty'<\/span>] * $item&#91;<span class=\"hljs-string\">'price'<\/span>];\n    },\n    <span class=\"hljs-number\">0<\/span>\n);\n\n<span class=\"hljs-keyword\">echo<\/span> $total; <span class=\"hljs-comment\">\/\/ 155<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/phptutorial.net\/playground\/?q=PD9waHAKCiRjYXJ0cyA9IFtdOwoKJHRvdGFsID0gYXJyYXlfcmVkdWNlKAogICAgJGNhcnRzLAogICAgZnVuY3Rpb24gKCRwcmV2LCAkaXRlbSkgewogICAgICAgIHJldHVybiAkcHJldiArICRpdGVtWydxdHknXSAqICRpdGVtWydwcmljZSddOwogICAgfSwKICAgIDAKKTsKCmVjaG8gJHRvdGFsOyAvLyAxNTU\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-number\">0<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Since the <code>$carts<\/code> array is empty, the total is zero.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the PHP <code>array_reduce()<\/code> function to reduce an array to a single value using a callback function.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Did you find this tutorial useful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"1111\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-array_reduce\/\"\n\t\t\t\tdata-post-title=\"PHP array_reduce Function\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"1111\"\n\t\t\t\tdata-post-url=\"https:\/\/www.phptutorial.net\/php-tutorial\/php-array_reduce\/\"\n\t\t\t\tdata-post-title=\"PHP array_reduce Function\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use the PHP array_reduce() function to reduce an array to a single value.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":15,"menu_order":72,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-1111","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1111","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/comments?post=1111"}],"version-history":[{"count":5,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1111\/revisions"}],"predecessor-version":[{"id":3141,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/1111\/revisions\/3141"}],"up":[{"embeddable":true,"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/pages\/15"}],"wp:attachment":[{"href":"https:\/\/www.phptutorial.net\/wp-json\/wp\/v2\/media?parent=1111"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}