Christian Seiler: Namen von Arrayelementen

Beitrag lesen

Hallo Peter,

Folgendes Beispiel:
<?php
    $foo['bar'] = 'SelfHTML';
    echo $foo[0];   // Schreibt 'SelfHTML'.
?>

Bei mir: (PHP 4.3.0)

christian@midnight:~$ php test.php
PHP Notice:  Undefined offset:  0 in /var/web/userdirs/christian/projekte/personen_db/tmp/test.php on line 3
christian@midnight:~$

Wir haben jetzt also eine Variable $foo[0] - alternativ $foo['bar'].

Ich nicht.

Dass diese Information zugänglich ist, sieht man an einem var_dump
des Arrays.

<?php
  $foo['bar'] = 'SelfHTML';
  var_dump ($foo);
?>

ergibt:

christian@midnight:~$ php test.php
array(1) {
  ["bar"]=>
  string(8) "SelfHTML"
}
christian@midnight:~$

Hintergrund: Ich möchte ein Kontaktformular schreiben, welches Zeilen
  in Form von 'name des <input>: value des <input>'.

Wenn Du einen assoziativen Array abarbeiten willst:

Methode 1:
while (list ($key, $value)) = each ($array)) {
  echo "$key: $value\n";
}

Methode 2:
foreach ($array as $key => $value) {
  echo "$key: $value\n";
}

Methode 3:
foreach (array_keys ($array) as $key) {
  $value = $array[$key];
  echo "$key: $value\n";
}

Christian

--
Hast Du einen Beitrag? Nur her damit!
http://aktuell.de.selfhtml.org/tippstricks/beitrag.htm
SELF-Code: (http://emmanuel.dammerer.at/selfcode.html)
sh:) fo:) ch:] rl:( br:> n4:& ie:% mo:) va:) de:] zu:) fl:( js:| ss:) ls:[