Map
Raxos\Collection\Map is a mutable, string keyed dictionary.
php
class Map implements
DebuggableInterface,
MapInterface,
MutableMapInterface,
JsonSerializable,
SerializableInterfaceConstruction
__construct
php
public function __construct(array $data = [])Creates a map from a plain associative array.
Methods
get
php
public function get(string $key, mixed $default = null): mixedReturns the value at the key, or the default when the key is absent.
has
php
public function has(string $key): boolReturns whether a value exists at the key.
set
php
public function set(string $key, mixed $value): voidSets the value at the key.
unset
php
public function unset(string $key): voidRemoves the value at the key.
merge
php
public function merge(MapInterface|array $other): staticMerges another map or array into this one in place and returns it. Keys already present keep their existing value.
toArray
php
public function toArray(): arrayReturns the underlying data as a plain array.
Access and serialization
Map implements Countable, IteratorAggregate and JsonSerializable, so count(), foreach and json_encode all work directly.
Example
php
<?php
declare(strict_types=1);
use Raxos\Collection\Map;
$headers = new Map([
'content-type' => 'application/json',
]);
$headers->set('x-request-id', 'abc-123');
$headers->has('content-type'); // true
$headers->get('accept', '*/*'); // '*/*'
$headers->toArray();
// ['content-type' => 'application/json', 'x-request-id' => 'abc-123']See also the maps concept page.