Free cookie consent management tool by TermsFeed Generator PHP Array Reduce | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Reduce

PHP Array Reduce

Last Updated on Feb 14, 2023

Array Reduce

Array reduce is a useful function that reduces the given array to a single value using a callback function. The callback function gets 2 arguments.

  1. carry holds the return value of the previous iteration
  2. item is the current item of the array

Let’s see an example 

$array = [1, 2, 3, 5, 10, 15];
$reduced = array_reduce($array, function ($carry, $item) {
   $carry += $item;
   return $carry;
});
echo $reduced;

Here I add all the values inside the array.

I should say that if you want to simply sum all the values or calculate the product of all the values you can simply use array_sum and array_product instead

echo array_sum($array);
// 36
echo array_product($array);
// 4500

But if you want to do something else, reduce is your friend.

Conclusion

Now you know about array reduce in PHP.

I recommend you to open a PHP files define arrays with different values try to reduce all of them to a single value with the help of array reduce function.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • array reduce in php
  • all the values of an array to a single value

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP Variables
Mar 21, 2023 programming

PHP Variables

Today we are going to learn about variables in PHP. Variables are very important and powerful in any programming language. So if you are learning PHP it's very important to know about variables. ...

5 Min Read Read More
PHP Callback Functions
Jan 08, 2023 programming

PHP Callback Functions

Today we are going to talk about callback functions in PHP. sometimes we need to pass a function as an argument of another function, that's exactly what callback function is. ...

7 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...