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