Mailinglisten-Archive |
Egon Schmid wrote:
> Ansonsten ist das folgende
> korrektes PHP3 (und wahrscheinlich aus dem Handbuch abgeschrieben).
>
> > arsort($Wert3);
> > for(reset($Wert3); $index=key($Wert3); next($Wert3))
> > {
> > print("$Text[$index] : $Wert1[$index] : $Wert2[$index] : $Wert3[$index]
> > <BR>\n");
> > }
> > ?>
>
kk_(at)_land:~ > Source/php3/php
<?php
$a[-2] = 20;
$a[2] = 10;
$a[3] = 1000;
$a[4] = -10;
$a[5] = 17;
$a[0] = 0;
for (reset($a); $i = key($a); next($a)) { print "$i $a[$i]\n"; }
Content-type: text/html
-2 20
2 10
3 1000
4 -10
5 17
arsort($a);
for (reset($a); $i = key($a); next($a)) { print "$i $a[$i]\n"; }
3 1000
-2 20
5 17
2 10
^D
1. Definiere ein Array mit verschiedenen Arrayindeces und Werten.
2. Gebe das Array aus. Conspiciously absent: $a[0]. Why?
key($a) für $a[0] ist 0. key($a) für $a[arrayende] ist false.
Beide Werte sind in PHP ununterscheidbar (nicht so in Perl).
3. Nach dem arsort($a) steht $a[0] irgendwo in der Mitte des Arrays,
die Arrayelemente nach $a[0] sind mit der o.a. Schleife nicht
mehr erreichbar, weil $a[0] falsch als Arrayende erkannt wird.
4. Die einzig korrekte Weise in PHP ein Array zu durchlaufen ist
while(list($k, $v) = each($a)) { print "$k $v\n$ }
Das ergibt dann korrekt:
arsort($a);
while(list($k, $v) = each($a)) { print "$k, $v\n"; }
3, 1000
-2, 20
5, 17
2, 10
0, 0
4, -10
Perl hat dieses Problem nicht. Es unterscheidet undef und 0.
Kristian
--
Kristian Köhntopp, NetUSE Kommunikationstechnologie GmbH
Siemenswall, D-24107 Kiel, Germany, +49 431 386 436 00
Using PHP3? See our web development library at
http://phplib.shonline.de/ (GPL)
php::bar PHP Wiki - Listenarchive