1--TEST--
2Test array_map() function : usage variations - associative array with different values
3--FILE--
4<?php
5/* Prototype  : array array_map  ( callback $callback  , array $arr1  [, array $...  ] )
6 * Description: Applies the callback to the elements of the given arrays
7 * Source code: ext/standard/array.c
8 */
9
10/*
11 * Test array_map() by passing associative array with different values for $arr1 argument
12 */
13
14echo "*** Testing array_map() : associative array with diff. values for 'arr1' argument ***\n";
15
16function callback($a)
17{
18  return ($a);
19}
20//get an unset variable
21$unset_var = array(1, 2);
22unset ($unset_var);
23
24//get a resource variable
25$fp = fopen(__FILE__, "r");
26
27//get a class
28class classA
29{
30  public function __toString(){
31    return "Class A object";
32  }
33}
34
35// get a heredoc string
36$heredoc = <<<EOT
37Hello world
38EOT;
39
40// initializing the array
41$arrays = array (
42
43       // empty array
44/*1*/  array(),
45
46       // arrays with integer values
47       array('0' => 0),
48       array("1" => 1),
49       array("one" => 1, 'two' => 2, "three" => 3, 4 => 4),
50
51       // arrays with float values
52/*5*/  array("float" => 2.3333),
53       array("f1" => 1.2, 'f2' => 3.33, 3 => 4.89999922839999, 'f4' => 33333333.3333),
54
55       // arrays with string values
56       array(111 => "\tHello", "red" => "col\tor", 2 => "\v\fworld", 3.3 =>  "pen\n"),
57/*8*/  array(111 => '\tHello', "red" => 'col\tor', 2 => '\v\fworld', 3.3 =>  'pen\n'),
58       array(1 => "hello", "heredoc" => $heredoc),
59
60       // array with object, unset variable and resource variable
61       array(11 => new classA(), "unset" => @$unset_var, "resource" => $fp),
62
63       // array with mixed values
64/*11*/ array(1 => 'hello', 2 => new classA(), 222 => "fruit",
65             'resource' => $fp, "int" => 133, "float" => 444.432,
66             "unset" => @$unset_var, "heredoc" => $heredoc)
67);
68
69// loop through the various elements of $arrays to test array_map()
70$iterator = 1;
71foreach($arrays as $arr1) {
72  echo "-- Iteration $iterator --\n";
73  var_dump( array_map('callback', $arr1) );
74  $iterator++;
75}
76
77echo "Done";
78?>
79--EXPECTF--
80*** Testing array_map() : associative array with diff. values for 'arr1' argument ***
81-- Iteration 1 --
82array(0) {
83}
84-- Iteration 2 --
85array(1) {
86  [0]=>
87  int(0)
88}
89-- Iteration 3 --
90array(1) {
91  [1]=>
92  int(1)
93}
94-- Iteration 4 --
95array(4) {
96  ["one"]=>
97  int(1)
98  ["two"]=>
99  int(2)
100  ["three"]=>
101  int(3)
102  [4]=>
103  int(4)
104}
105-- Iteration 5 --
106array(1) {
107  ["float"]=>
108  float(2.3333)
109}
110-- Iteration 6 --
111array(4) {
112  ["f1"]=>
113  float(1.2)
114  ["f2"]=>
115  float(3.33)
116  [3]=>
117  float(4.8999992284)
118  ["f4"]=>
119  float(33333333.3333)
120}
121-- Iteration 7 --
122array(4) {
123  [111]=>
124  string(6) "	Hello"
125  ["red"]=>
126  string(6) "col	or"
127  [2]=>
128  string(7) "world"
129  [3]=>
130  string(4) "pen
131"
132}
133-- Iteration 8 --
134array(4) {
135  [111]=>
136  string(7) "\tHello"
137  ["red"]=>
138  string(7) "col\tor"
139  [2]=>
140  string(9) "\v\fworld"
141  [3]=>
142  string(5) "pen\n"
143}
144-- Iteration 9 --
145array(2) {
146  [1]=>
147  string(5) "hello"
148  ["heredoc"]=>
149  string(11) "Hello world"
150}
151-- Iteration 10 --
152array(3) {
153  [11]=>
154  object(classA)#%d (0) {
155  }
156  ["unset"]=>
157  NULL
158  ["resource"]=>
159  resource(%d) of type (stream)
160}
161-- Iteration 11 --
162array(8) {
163  [1]=>
164  string(5) "hello"
165  [2]=>
166  object(classA)#%d (0) {
167  }
168  [222]=>
169  string(5) "fruit"
170  ["resource"]=>
171  resource(%d) of type (stream)
172  ["int"]=>
173  int(133)
174  ["float"]=>
175  float(444.432)
176  ["unset"]=>
177  NULL
178  ["heredoc"]=>
179  string(11) "Hello world"
180}
181Done
182