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 xml and json
Mar 21, 2023 programming

PHP xml and json

Today we are going to talk about working XML and JSON in PHP. XML and JSON are the most common formats that are used in APIs. knowing how to work with them is essential for any web developer. ...

9 Min Read Read More
PHP Abstract Class
Mar 22, 2023 programming

PHP Abstract Class

Today we are going to talk about abstract classes in PHP. Abstract class is another important topic in object oriented programming and today we are going to learn everything about them. ...

8 Min Read Read More
PHP compact
Feb 14, 2023 programming

PHP compact

Today we are going to talk about compact function in PHP. Compact is one of those functions that helps the readability of your code a lot and also reduces the lines of code. ...

4 Min Read Read More
A Programmer's Guide to Debugging: Essential Steps to Follow
Mar 23, 2024 programming

A Programmer's Guide to Debugging: Essential Steps to Follow

Debugging is a crucial skill for any programmer, as it helps identify and fix issues in the code. Effective debugging not only improves the overall quality of your software but can also save you time and frustration. ...

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