1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
--TEST--
Test array_map() function : usage variations - callback function with different return types
--FILE--
<?php
/* Prototype : array array_map ( callback $callback , array $arr1 [, array $... ] )
* Description: Applies the callback to the elements of the given arrays
* Source code: ext/standard/array.c
*/
/*
* Test array_map() by passing different callback function returning:
* int, string, bool, null values
*/
echo "*** Testing array_map() : callback with diff return value ***\n";
$array1 = array(1, 2, 3);
$array2 = array(3, 4, 5);
echo "-- with integer return value --\n";
function callback_int($a, $b)
{
return $a + $b;
}
var_dump( array_map('callback_int', $array1, $array2));
echo "-- with string return value --\n";
function callback_string($a, $b)
{
return "$a"."$b";
}
var_dump( array_map('callback_string', $array1, $array2));
echo "-- with bool return value --\n";
function callback_bool($a, $b)
{
return TRUE;
}
var_dump( array_map('callback_bool', $array1, $array2));
echo "-- with null return value --\n";
function callback_null($array1)
{
return NULL;
}
var_dump( array_map('callback_null', $array1));
echo "-- with no return value --\n";
function callback_without_ret($arr1)
{
echo "callback_without_ret called\n";
}
var_dump( array_map('callback_without_ret', $array1));
echo "Done";
?>
--EXPECTF--
*** Testing array_map() : callback with diff return value ***
-- with integer return value --
array(3) {
[0]=>
int(4)
[1]=>
int(6)
[2]=>
int(8)
}
-- with string return value --
array(3) {
[0]=>
string(2) "13"
[1]=>
string(2) "24"
[2]=>
string(2) "35"
}
-- with bool return value --
array(3) {
[0]=>
bool(true)
[1]=>
bool(true)
[2]=>
bool(true)
}
-- with null return value --
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
}
-- with no return value --
callback_without_ret called
callback_without_ret called
callback_without_ret called
array(3) {
[0]=>
NULL
[1]=>
NULL
[2]=>
NULL
}
Done
|