Home > Article > Backend Development > php array function (3)
Six: The product and sum functions within the array array_sum($array
);array_product($array
);
<code><span>$arr</span> = <span>array</span>(<span>"2"</span>,<span>"3"</span>,<span>"4"</span>); <span>$sum</span> = array_sum(<span>$arr</span>); <span>$product</span> = array_product(<span>$arr</span>); <span>print</span>(<span>$sum</span>.<span>"<br/>"</span>); <span>print</span>(<span>$product</span>);</code>
The result is as shown below
Two summation and product functions In the function, if there is a non-numeric cell in the array, this cell is counted as zero. Therefore, for the multiplication of arrays containing non-numeric units, the result is 0; as shown in the figure below
<code><span>$arr</span> = <span>array</span>(<span>"2"</span>,<span>"3"</span>,<span>"4"</span>,<span>"a"</span>); <span>$sum</span> = array_sum(<span>$arr</span>); <span>$product</span> = array_product(<span>$arr</span>); <span>print</span>(<span>$sum</span>.<span>"<br/>"</span>); <span>print</span>(<span>$product</span>);</code>
The above introduces the PHP array function (3), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.