1--TEST--
2Test array_reduce() function
3--INI--
4precision=14
5--FILE--
6<?php
7/* Prototype: array array_reduce(array $array, mixed $callback, mixed $initial);
8   Description: Iteratively reduce the array to a single value via the callback
9*/
10
11$array = array('foo', 'foo', 'bar', 'qux', 'qux', 'quux');
12
13echo "\n*** Testing array_reduce() to integer ***\n";
14function reduce_int($w, $v) { return $w + strlen($v); }
15$initial = 42;
16var_dump(array_reduce($array, 'reduce_int', $initial), $initial);
17
18echo "\n*** Testing array_reduce() to float ***\n";
19function reduce_float($w, $v) { return $w + strlen($v) / 10; }
20$initial = 4.2;
21var_dump(array_reduce($array, 'reduce_float', $initial), $initial);
22
23echo "\n*** Testing array_reduce() to string ***\n";
24function reduce_string($w, $v) { return $w . $v; }
25$initial = 'quux';
26var_dump(array_reduce($array, 'reduce_string', $initial), $initial);
27
28echo "\n*** Testing array_reduce() to array ***\n";
29function reduce_array($w, $v) { $w[$v]++; return $w; }
30$initial = array('foo' => 42, 'bar' => 17, 'qux' => -2, 'quux' => 0);
31var_dump(array_reduce($array, 'reduce_array', $initial), $initial);
32
33echo "\n*** Testing array_reduce() to null ***\n";
34function reduce_null($w, $v) { return $w . $v; }
35$initial = null;
36var_dump(array_reduce($array, 'reduce_null', $initial), $initial);
37
38echo "\nDone";
39?>
40--EXPECTF--
41*** Testing array_reduce() to integer ***
42int(61)
43int(42)
44
45*** Testing array_reduce() to float ***
46float(6.1)
47float(4.2)
48
49*** Testing array_reduce() to string ***
50string(23) "quuxfoofoobarquxquxquux"
51string(4) "quux"
52
53*** Testing array_reduce() to array ***
54array(4) {
55  ["foo"]=>
56  int(44)
57  ["bar"]=>
58  int(18)
59  ["qux"]=>
60  int(0)
61  ["quux"]=>
62  int(1)
63}
64array(4) {
65  ["foo"]=>
66  int(42)
67  ["bar"]=>
68  int(17)
69  ["qux"]=>
70  int(-2)
71  ["quux"]=>
72  int(0)
73}
74
75*** Testing array_reduce() to null ***
76string(19) "foofoobarquxquxquux"
77NULL
78
79Done
80