Questions tagged [array-reduce]

An array reduce function allows you to iteratively reduce an array to a single value using a callback function. PHP: array_reduce(), JavaScript: Array.prototype.reduce()

An array reduce function allows you to implement a algorithm by using a callback function to iteratively reduce an array to a single value.

Documentation

Example

Consider this simple code, which will result in calculating a total value of 15 by calling the sum() function 5 times.

PHP:

function sum($total, $item) {
    return $total + $item;
}

$data = array(1, 2, 3, 4, 5);

$result = array_reduce($data, 'sum', 0);

JavaScript:

const sum = (total, item) => total + item;

const data = [1, 2, 3, 4, 5];

const result = data.reduce(sum, 0);

The sum() function will be called 5 times, once for each element of the array:

  • The first time, it uses the starting value of 0 that we provided, and processes the first item:

      sum(0, 1) // returns 1
    
  • The second time, it uses the return value from the first call, and processes the second item:

      sum(1, 2) // returns 3
    
  • Each time after that, it uses the previous return value, and processes the next item in the array:

      sum(3, 3) // return 6
      sum(6, 4) // returns 10
      sum(10, 5) // returns 15
    
  • And finally, the value returned from the last call is returned back as the result of the array_reduce() function:

      $result = 15;
    
58 questions
27
votes
8 answers

In PHP, is there a function that returns an array made up of the value of a key from an array of associative arrays?

I'm sure this question has been asked before, my apologies for not finding it first. The original array: [0] => Array ( [categoryId] => 1 [eventId] => 2 [eventName] => 3 [vendorName] => 4 ) [1] => Array …
Caleb Gray
  • 3,040
  • 2
  • 21
  • 32
23
votes
3 answers

Is there a way to break from an array's reduce function in Swift?

Is there a way to do something similar to break from a for loop but in array's reduce() function? E.g. consider I have an array: var flags = [false, false, true, false, false, true, false] ... and I need to get a cumulative || on them. With a for…
0x416e746f6e
  • 9,872
  • 5
  • 40
  • 68
13
votes
18 answers

How to convert multi-dimensional array into single array using PHP?

After implementing database queries, I am getting the multi-dimensional array below. Two Dimensional Array Array ( [0] => Array ( [t1] => test1 ) [1] => Array ( [t2] => test2 ) …
Karan Adhikari
  • 485
  • 1
  • 5
  • 16
13
votes
5 answers

How to exit from array iteration functions (array_reduce) in PHP

I'm having an array_reduce function which I am willing to exit when specific criteria is met. $result = array_reduce($input, function($carrier, $item) { // do the $carrier stuff if (/* god was one of us */) { break; //some break analogue …
Konstantin Bodnia
  • 1,372
  • 3
  • 20
  • 43
9
votes
1 answer

How can I include a variable inside a callback function?

I'm trying to get counts of array values greater than n. I'm using array_reduce() like so: $arr = range(1,10); echo array_reduce($arr, function ($a, $b) { return ($b > 5) ? ++$a : $a; }); This prints the number of elements in the array greater than…
Ryan
  • 14,682
  • 32
  • 106
  • 179
4
votes
1 answer

Multiplying array indexes in PHP with array_reduce

Why does the array_reduce() method work differently when adding and multiplying? When I add the array values below, the code produces the expected result: 15. But when I multiply, it returns: 0. Same code... The only difference is that the + sign is…
shmuli
  • 5,086
  • 4
  • 32
  • 64
4
votes
1 answer

How to check if all values in multidimensional array are empty

I have a form posting a multidimensional array to my PHP script, I need to know if all the values in the array are empty or not. Here is my array: $array[] = array('a'=>'', 'b'=>array('x'=>''), …
littleRoom
  • 81
  • 2
  • 5
4
votes
1 answer

Did O'reilly make a mistake: array_reduce

I am learning PHP from O'reilly media book 'Programing PHP' and I stumbled upon this: function add_up ($running_total, $current_value) { $running_total += $current_value * $current_value; return $running_total; } $numbers = array(2, 3, 5,…
boksa
  • 223
  • 3
  • 6
3
votes
5 answers

Sum each row's deep values in a 3-dimensional array

I have need to calculate the sum of the deep elements foreach first level element. Sample 3-level array: [ 1 => [ 'A' => ['AA' => 3, 'AB' => 5], 'B' => ['BA' => 2] ], 2 => [ 'C' => ['CA' => 4], 'D' =>…
payket
  • 137
  • 7
3
votes
2 answers

JavaScript code to get count of occurrence of objects in array of objects using array.reduce()

var developers = [ { name: "Joe", age: 23 ,overallLevel: "high"}, { name: "Sue", age: 28 ,overallLevel: "advanced" }, { name: "Jon", age: 32 ,overallLevel: "high" }, { name: "Bob", age: 24 ,overallLevel: "high" }, { name: "Bob",…
Saily Jadhav
  • 149
  • 2
  • 10
3
votes
3 answers

Workarounds for PHP's array_reduce integer third parameter

For some or other reason, the array_reduce function in PHP only accepts integers as it's third parameter. This third parameter is used as a starting point in the whole reduction process: function int_reduc($return, $extra) { return $return +…
Jrgns
  • 24,699
  • 18
  • 71
  • 77
2
votes
3 answers

Array of objects - how to group items by property and squash data from each of grouped items into 1

I am trying to perfom operation on array that is described in topic. What i've done already - group by common property + map data of each item to desired format. It was achieved by using reduce + map function inside it. Now i need final part that is…
D.Wasilewski
  • 119
  • 3
  • 3
  • 12
2
votes
2 answers

Getting sum of total value from Reduce

I have 3 label that can receive input when button clicked and then i need sum from the third input value the code kinda work but instead of getting the sum of the third input it instead it looks like the image below where the input get combined…
2
votes
5 answers

Javascript array restructuring for nested array

I have an array of objects as mentioned below. const inputArray =[ { name: "Energy", quantity: [ { qval: "100 ", unit: "unit1" }, { qval: "200 ", unit: "unit2" } ], }, { …
Shah
  • 79
  • 6
2
votes
1 answer

Condition inside array_reduce

I want to make condition out of the following array, but it does not give my expected result because it does not run the second condition. $arr = [ [ 472 => [ 'EL' => 52.9, 'MT' => 57.375, 'MO'…
1
2 3 4