Ds\Map::put
(PECL ds >= 1.0.0)
Ds\Map::put — Associates a key with a value
Beschreibung
Associates a key
with a value
,
overwriting a previous association if one exists.
Hinweis:
Keys of type object are supported. If an object implements Ds\Hashable, equality will be determined by the object's
equals
function. If an object does not implement Ds\Hashable, objects must be references to the same instance to be considered equal.
Hinweis:
You can also use array syntax to associate values by key, eg.
$map["key"] = $value
.
Be careful when using array syntax. Scalar keys will be coerced to
integers by the engine. For example, $map["1"]
will attempt
to access int(1)
, while $map->get("1")
will
correctly look up the string key.
See Arrays.
Parameter-Liste
-
key
-
The key to associate the value with.
-
value
-
The value to be associated with the key.
Beispiele
Beispiel #1 Ds\Map::put() example
<?php
$map = new \Ds\Map();
$map->put("a", 1);
$map->put("b", 2);
$map->put("c", 3);
print_r($map);
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
Ds\Map Object ( [0] => Ds\Pair Object ( [key] => a [value] => 1 ) [1] => Ds\Pair Object ( [key] => b [value] => 2 ) [2] => Ds\Pair Object ( [key] => c [value] => 3 ) )
Beispiel #2 Ds\Map::put() example using objects as keys
<?php
class HashableObject implements \Ds\Hashable
{
/**
* An arbitrary value to use as the hash value. Does not define equality.
*/
private $value;
public function __construct($value)
{
$this->value = $value;
}
public function hash()
{
return $this->value;
}
public function equals($obj): bool
{
return $this->value === $obj->value;
}
}
$map = new \Ds\Map();
$obj = new \ArrayIterator([]);
// Using the same instance multiple times will overwrite the previous value.
$map->put($obj, 1);
$map->put($obj, 2);
// Using multiple instances of the same object will create new associations.
$map->put(new \stdClass(), 3);
$map->put(new \stdClass(), 4);
// Using multiple instances of equal hashable objects will overwrite previous values.
$map->put(new \HashableObject(1), 5);
$map->put(new \HashableObject(1), 6);
$map->put(new \HashableObject(2), 7);
$map->put(new \HashableObject(2), 8);
var_dump($map);
?>
Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:
object(Ds\Map)#1 (5) { [0]=> object(Ds\Pair)#7 (2) { ["key"]=> object(ArrayIterator)#2 (1) { ["storage":"ArrayIterator":private]=> array(0) { } } ["value"]=> int(2) } [1]=> object(Ds\Pair)#8 (2) { ["key"]=> object(stdClass)#3 (0) { } ["value"]=> int(3) } [2]=> object(Ds\Pair)#9 (2) { ["key"]=> object(stdClass)#4 (0) { } ["value"]=> int(4) } [3]=> object(Ds\Pair)#10 (2) { ["key"]=> object(HashableObject)#5 (1) { ["value":"HashableObject":private]=> int(1) } ["value"]=> int(6) } [4]=> object(Ds\Pair)#11 (2) { ["key"]=> object(HashableObject)#6 (1) { ["value":"HashableObject":private]=> int(2) } ["value"]=> int(8) } }
Deutsche Übersetzung
Sie haben gebeten, diese Seite auf Deutsch zu besuchen. Momentan ist nur die Oberfläche übersetzt, aber noch nicht der gesamte Inhalt.Wenn Sie mir bei Übersetzungen helfen wollen, ist Ihr Beitrag willkommen. Alles, was Sie tun müssen, ist, sich auf der Website zu registrieren und mir eine Nachricht zu schicken, in der Sie gebeten werden, Sie der Gruppe der Übersetzer hinzuzufügen, die Ihnen die Möglichkeit gibt, die gewünschten Seiten zu übersetzen. Ein Link am Ende jeder übersetzten Seite zeigt an, dass Sie der Übersetzer sind und einen Link zu Ihrem Profil haben.
Vielen Dank im Voraus.
Dokument erstellt 30/01/2003, zuletzt geändert 26/10/2018
Quelle des gedruckten Dokuments:https://www.gaudry.be/de/php-rf-ds-map.put.html
Die Infobro ist eine persönliche Seite, deren Inhalt in meiner alleinigen Verantwortung liegt. Der Text ist unter der CreativeCommons-Lizenz (BY-NC-SA) verfügbar. Weitere Informationen auf die Nutzungsbedingungen und dem Autor.
Referenzen
Diese Verweise und Links verweisen auf Dokumente, die während des Schreibens dieser Seite konsultiert wurden, oder die zusätzliche Informationen liefern können, aber die Autoren dieser Quellen können nicht für den Inhalt dieser Seite verantwortlich gemacht werden.
Der Autor Diese Website ist allein dafür verantwortlich, wie die verschiedenen Konzepte und Freiheiten, die mit den Nachschlagewerken gemacht werden, hier dargestellt werden. Denken Sie daran, dass Sie mehrere Quellinformationen austauschen müssen, um das Risiko von Fehlern zu reduzieren.