guter geist: Wie an ein assoziatives Array Werte anhängen

Hallo,
wie kann ich an ein bestehendes assoziatives Array weitere assoziative Wertpaare anhängen ohne dass die assoziativen Keys verloren gehen?

Folgendes Beispiel fabriziert numerische Keys:

<?
$myarray = array("10023" => "Doris");
$myarray2 = array("11245" => "Manfred");

$myarray = array_merge($myarray, $myarray2);

foreach ($myarray as $key=>$val)
{
echo "key = ".$key." val = ".$val."<BR>";
}
?>

Ergebnis:
key = 0 val = Doris
key = 1 val = Manfred

Dumm gelaufen :-( Die Keys 10023 und 11245 sind weg.

Was kann ich dagegen machen? Vor allem müssen die Arraywerte am Ende auch in folgender Form ausgegeben werden können (nicht nur in einer foreach-Schleife):

echo $myarray[11245];

Danke für Eure Bemühungen.

  1. Moin,

    Was kann ich dagegen machen? Vor allem müssen die Arraywerte am Ende auch in folgender Form ausgegeben werden können (nicht nur in einer foreach-Schleife):

    Schau halt in die Doku von array_merge():
    |  If you want to completely preserve the arrays and just want to append them
    |  to each other, use the + operator:
    |
    | $array1 = array();
    | $array2 = array(1 => "data");
    | $result = $array1 + $array2;
    |
    | The numeric key will be preserved and thus the association remains.

    --
    Henryk Plötz
    Grüße aus Berlin
    ~~~~~~~~ Un-CDs, nein danke! http://www.heise.de/ct/cd-register/ ~~~~~~~~
    ~~ Help Microsoft fight software piracy: Give Linux to a friend today! ~~
    1. Danke, fuktioniert jetzt.