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

PHP Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP session
Mar 21, 2023 programming

PHP session

Today we are going to learn about session in PHP. Sessions are like cookies but instead of storing the key value pairs on the user’s computer, session stores them on the server. ...

8 Min Read Read More
PHP upload file
Feb 15, 2023 programming

PHP upload file

Today we are going to talk about uploading a file in PHP. Uploading a file is simple but it's very important to know how to handle uploading the files correctly and securely. ...

7 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 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 ...