Return to array_reduce.phpt CVS log | Up to [ELWIX - Embedded LightWeight unIX -] / embedaddon / php / ext / standard / tests / array |
1.1 misho 1: --TEST-- 2: Test array_reduce() function 3: --INI-- 4: precision=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: 13: echo "\n*** Testing array_reduce() to integer ***\n"; 14: function reduce_int($w, $v) { return $w + strlen($v); } 15: $initial = 42; 16: var_dump(array_reduce($array, 'reduce_int', $initial), $initial); 17: 18: echo "\n*** Testing array_reduce() to float ***\n"; 19: function reduce_float($w, $v) { return $w + strlen($v) / 10; } 20: $initial = 4.2; 21: var_dump(array_reduce($array, 'reduce_float', $initial), $initial); 22: 23: echo "\n*** Testing array_reduce() to string ***\n"; 24: function reduce_string($w, $v) { return $w . $v; } 25: $initial = 'quux'; 26: var_dump(array_reduce($array, 'reduce_string', $initial), $initial); 27: 28: echo "\n*** Testing array_reduce() to array ***\n"; 29: function reduce_array($w, $v) { $w[$v]++; return $w; } 30: $initial = array('foo' => 42, 'bar' => 17, 'qux' => -2, 'quux' => 0); 31: var_dump(array_reduce($array, 'reduce_array', $initial), $initial); 32: 33: echo "\n*** Testing array_reduce() to null ***\n"; 34: function reduce_null($w, $v) { return $w . $v; } 35: $initial = null; 36: var_dump(array_reduce($array, 'reduce_null', $initial), $initial); 37: 38: echo "\nDone"; 39: ?> 40: --EXPECTF-- 41: *** Testing array_reduce() to integer *** 42: int(61) 43: int(42) 44: 45: *** Testing array_reduce() to float *** 46: float(6.1) 47: float(4.2) 48: 49: *** Testing array_reduce() to string *** 50: string(23) "quuxfoofoobarquxquxquux" 51: string(4) "quux" 52: 53: *** Testing array_reduce() to array *** 54: array(4) { 55: ["foo"]=> 56: int(44) 57: ["bar"]=> 58: int(18) 59: ["qux"]=> 60: int(0) 61: ["quux"]=> 62: int(1) 63: } 64: array(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 *** 76: string(19) "foofoobarquxquxquux" 77: NULL 78: 79: Done