function.array-map.html
21 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Wendet eine Callback-Funktion auf die Elemente von Arrays an</title>
</head>
<body><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="function.array-keys.html">array_keys</a></div>
<div class="next" style="text-align: right; float: right;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
<div class="up"><a href="ref.array.html">Array Funktionen</a></div>
<div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-map" class="refentry">
<div class="refnamediv">
<h1 class="refname">array_map</h1>
<p class="verinfo">(PHP 4 >= 4.0.6, PHP 5)</p><p class="refpurpose"><span class="refname">array_map</span> — <span class="dc-title">Wendet eine Callback-Funktion auf die Elemente von Arrays an</span></p>
</div>
<div class="refsect1 description" id="refsect1-function.array-map-description">
<h3 class="title">Beschreibung</h3>
<div class="methodsynopsis dc-description">
<span class="type">array</span> <span class="methodname"><strong>array_map</strong></span>
( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.callback" class="type callback">callback</a></span> <code class="parameter">$callback</code></span>
, <span class="methodparam"><span class="type">array</span> <code class="parameter">$arr1</code></span>
[, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span>
] )</div>
<p class="para rdfs-comment">
<span class="function"><strong>array_map()</strong></span> gibt ein Array mit allen Elementen von
<code class="parameter">arr1</code> zurück, nachdem die Callback-Funktion
<code class="parameter">callback</code> auf jedes einzelne Element angewandt
wurde. Die Anzahl Parameter, welche die Callback-Funktion
<code class="parameter">callback</code> akzeptiert, sollte der Anzahl der an
<span class="function"><strong>array_map()</strong></span> übergebenen Arrays entsprechen.
</p>
</div>
<div class="refsect1 parameters" id="refsect1-function.array-map-parameters">
<h3 class="title">Parameter-Liste</h3>
<p class="para">
<dl>
<dt>
<code class="parameter">callback</code></dt>
<dd>
<p class="para">
Callback-Funktion, die für jedes Element in jedem Array
aufgerufen werden soll.
</p>
</dd>
<dt>
<code class="parameter">arr1</code></dt>
<dd>
<p class="para">
Ein Array, auf das die <code class="parameter">callback</code>-Funktion
angewendet werden soll.
</p>
</dd>
<dt>
<code class="parameter">array</code></dt>
<dd>
<p class="para">
Variable Liste von Array-Argumenten, auf die die
<code class="parameter">callback</code>-Funktion angewendet werden soll.
</p>
</dd>
</dl>
</p>
</div>
<div class="refsect1 returnvalues" id="refsect1-function.array-map-returnvalues">
<h3 class="title">Rückgabewerte</h3>
<p class="para">
Gibt ein Array zurück, dass alle Elemente von
<code class="parameter">arr1</code> enthält, nachdem die
<code class="parameter">callback</code>-Funktion auf jedes Element
angewendet wurde.
</p>
</div>
<div class="refsect1 examples" id="refsect1-function.array-map-examples">
<h3 class="title">Beispiele</h3>
<p class="para">
<div class="example" id="example-4916">
<p><strong>Beispiel #1 <span class="function"><strong>array_map()</strong></span>-Beispiel</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">cube</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">)<br />{<br /> return(</span><span style="color: #0000BB">$n </span><span style="color: #007700">* </span><span style="color: #0000BB">$n </span><span style="color: #007700">* </span><span style="color: #0000BB">$n</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cube"</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>
Dies speichert in <var class="varname"><var class="varname">$b</var></var>:
</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-4917">
<p><strong>Beispiel #2 <span class="function"><strong>array_map()</strong></span> mit einer Lambda-Funktion
(seit PHP 5.3.0)</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /><br /></span><span style="color: #FF8000">/* Seit PHP 5.3.0 */<br /><br /></span><span style="color: #0000BB">$func </span><span style="color: #007700">= function(</span><span style="color: #0000BB">$value</span><span style="color: #007700">) { return </span><span style="color: #0000BB">$value </span><span style="color: #007700">* </span><span style="color: #0000BB">2</span><span style="color: #007700">; };<br /><br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">$func</span><span style="color: #007700">, </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
</pre></div>
</div>
</div>
</p>
<p class="para">
<div class="example" id="example-4918">
<p><strong>Beispiel #3
<span class="function"><strong>array_map()</strong></span> - Verwendung mehrerer Arrays
</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">function </span><span style="color: #0000BB">show_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">, </span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br /> return(</span><span style="color: #DD0000">"Die Zahl </span><span style="color: #0000BB">$n</span><span style="color: #DD0000"> heißt auf Spanisch </span><span style="color: #0000BB">$m</span><span style="color: #DD0000">"</span><span style="color: #007700">);<br />}<br /><br />function </span><span style="color: #0000BB">map_Spanish</span><span style="color: #007700">(</span><span style="color: #0000BB">$n</span><span style="color: #007700">, </span><span style="color: #0000BB">$m</span><span style="color: #007700">)<br />{<br /> return(array(</span><span style="color: #0000BB">$n </span><span style="color: #007700">=> </span><span style="color: #0000BB">$m</span><span style="color: #007700">));<br />}<br /><br /></span><span style="color: #0000BB">$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">, </span><span style="color: #DD0000">"dos"</span><span style="color: #007700">, </span><span style="color: #DD0000">"tres"</span><span style="color: #007700">, </span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">, </span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"show_Spanish"</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"map_Spanish"</span><span style="color: #007700">, </span><span style="color: #0000BB">$a </span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
// Ausgabe von $c
Array
(
[0] => Die Zahl 1 heißt auf Spanisch uno
[1] => Die Zahl 2 heißt auf Spanisch dos
[2] => Die Zahl 3 heißt auf Spanisch tres
[3] => Die Zahl 4 heißt auf Spanisch cuatro
[4] => Die Zahl 5 heißt auf Spanisch cinco
)
// Ausgabe von $d
Array
(
[0] => Array
(
[1] => uno
)
[1] => Array
(
[2] => dos
)
[2] => Array
(
[3] => tres
)
[3] => Array
(
[4] => cuatro
)
[4] => Array
(
[5] => cinco
)
)
</pre></div>
</div>
</div>
</p>
<p class="para">
Bei Verwendung von zwei oder mehr Arrays sollten diese gewöhnlich
die gleiche Länge besitzen, da die Callback-Funktion parallel auf
die entsprechenden Elemente angewandt wird. Haben die Arrays
unterschiedliche Längen, wird das kürzeste um leere Elemente
erweitert.
</p>
<p class="para">
Eine interessante Anwendung dieser Funktion ist die Konstruktion
eines Arrays bestehend aus Arrays, was mit <strong><code>NULL</code></strong> als Name der
Callback-Funktion leicht realisiert werden kann.
</p>
<p class="para">
<div class="example" id="example-4919">
<p><strong>Beispiel #4 Erstellen eines Arrays mit Arrays</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"eins"</span><span style="color: #007700">, </span><span style="color: #DD0000">"zwei"</span><span style="color: #007700">, </span><span style="color: #DD0000">"drei"</span><span style="color: #007700">, </span><span style="color: #DD0000">"vier"</span><span style="color: #007700">, </span><span style="color: #DD0000">"fünf"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"uno"</span><span style="color: #007700">, </span><span style="color: #DD0000">"dos"</span><span style="color: #007700">, </span><span style="color: #DD0000">"tres"</span><span style="color: #007700">, </span><span style="color: #DD0000">"cuatro"</span><span style="color: #007700">, </span><span style="color: #DD0000">"cinco"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$d </span><span style="color: #007700">= </span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">, </span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$d</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Array
(
[0] => Array
(
[0] => 1
[1] => eins
[2] => uno
)
[1] => Array
(
[0] => 2
[1] => zwei
[2] => dos
)
[2] => Array
(
[0] => 3
[1] => drei
[2] => tres
)
[3] => Array
(
[0] => 4
[1] => vier
[2] => cuatro
)
[4] => Array
(
[0] => 5
[1] => fünf
[2] => cinco
)
)
</pre></div>
</div>
</div>
</p>
<p class="para">
Wenn das Array-Argument String-Schlüssel enthält, wird das
zurückgegebene Array String-Schlüssel enthalten sofern genau ein
Array übergeben wurde. Sollten mehrere Arrays übergeben werden,
so hat das zurückgegebene Array immer Integer-Schlüssel.
</p>
<p class="para">
<div class="example" id="example-4920">
<p><strong>Beispiel #5 <span class="function"><strong>array_map()</strong></span> - mit String-Schlüsseln</strong></p>
<div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"stringkey" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"value"</span><span style="color: #007700">);<br />function </span><span style="color: #0000BB">cb1</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">) {<br /> return array (</span><span style="color: #0000BB">$a</span><span style="color: #007700">);<br />}<br />function </span><span style="color: #0000BB">cb2</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">) {<br /> return array (</span><span style="color: #0000BB">$a</span><span style="color: #007700">, </span><span style="color: #0000BB">$b</span><span style="color: #007700">);<br />}<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb1"</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #DD0000">"cb2"</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_map</span><span style="color: #007700">(</span><span style="color: #0000BB">null</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">, </span><span style="color: #0000BB">$arr</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents"><p>Das oben gezeigte Beispiel erzeugt folgende
Ausgabe:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
array(1) {
["stringkey"]=>
array(1) {
[0]=>
string(5) "value"
}
}
array(1) {
[0]=>
array(2) {
[0]=>
string(5) "value"
[1]=>
string(5) "value"
}
}
array(1) {
["stringkey"]=>
string(5) "value"
}
array(1) {
[0]=>
array(2) {
[0]=>
string(5) "value"
[1]=>
string(5) "value"
}
}
</pre></div>
</div>
</div>
</p>
</div>
<div class="refsect1 seealso" id="refsect1-function.array-map-seealso">
<h3 class="title">Siehe auch</h3>
<p class="para">
<ul class="simplelist">
<li class="member"><span class="function"><a href="function.array-filter.html" class="function" rel="rdfs-seeAlso">array_filter()</a> - Filtert Elemente eines Arrays mittels einer Callback-Funktion</span></li>
<li class="member"><span class="function"><a href="function.array-reduce.html" class="function" rel="rdfs-seeAlso">array_reduce()</a> - Iterative Reduktion eines Arrays zu einem Wert mittels einer
Callbackfunktion</span></li>
<li class="member"><span class="function"><a href="function.array-walk.html" class="function" rel="rdfs-seeAlso">array_walk()</a> - Wendet eine Benutzerfunktion auf jedem Element eines Arrays an</span></li>
<li class="member"><span class="function"><a href="function.create-function.html" class="function" rel="rdfs-seeAlso">create_function()</a> - Erzeugen einer anonymen / temporären (Lambda-Stil) Funktion</span></li>
<li class="member">Informationen über den <a href="language.pseudo-types.html#language.types.callback" class="link">Callback</a>-Typ</li>
</ul>
</p>
</div>
</div><hr /><div class="manualnavbar" style="text-align: center;">
<div class="prev" style="text-align: left; float: left;"><a href="function.array-keys.html">array_keys</a></div>
<div class="next" style="text-align: right; float: right;"><a href="function.array-merge-recursive.html">array_merge_recursive</a></div>
<div class="up"><a href="ref.array.html">Array Funktionen</a></div>
<div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>