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 curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP Pass Arguments by Reference
Jan 12, 2023 programming

PHP Pass Arguments by Reference

Today we are going to talk about splat passing arguments by reference in PHP. By default when we pass argument to a function we pass it by value. ...

6 Min Read Read More
Top Skills for Developers: Non-Technical and Technical Essentials
Apr 05, 2023 programming

Top Skills for Developers: Non-Technical and Technical Essentials

Being a successful developer requires a combination of both technical and non-technical skills. ...

9 Min Read Read More
Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects
Jul 20, 2025 programming

Node.js Beginner to Advanced 2025: Everything You Need to Know to Build Modern Projects

Node.js has matured into one of the most essential tools in modern web development. Originally released in 2009, Node.js transformed JavaScript from a strictly frontend language into a robust backend solution capable of powering APIs, real-time applications, microservices, IoT systems, and more. ...

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