PHP code example of mfn / util-simpleorderedmap

1. Go to this page and download the library: Download mfn/util-simpleorderedmap library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mfn / util-simpleorderedmap example snippets


$map = new \Mfn\Util\Map\SimpleOrderedMap;
$map->add(new \stdClass, "value");
foreach ($map->keys() as $key) {
  echo $map->get($key), PHP_EOL;
}

use \Mfn\Util\Map\SimpleOrderedMap;
$map = SimpleOrderedMap;
$map->add($key, $val); # throws exception if key already exists
$map->set($key, $val); # replaces value if key already exists

$val = $map->get($key); # throws exception if key does not exist
if ($map->exists($key)) {
  # ...
}

$map->del($key); # throws exception if key does not exist

$index = $map->getKeyPosition($key);
$key = $map->getKayAt($index);
$val = $map->getValueAt($index);

use \Mfn\Util\Map\SimpleOrderedMap;
$map = SimpleOrderedMap::fromHash(['a'=>true,10=>NULL]);
$map->keys(); # ['a',10]
$map->values(): # [true,NULL]

# The same with separate arrays. Note: arrays length must match
$map = SimpleOrderedMap::fromArrays(
  ['a',10],
  [true,NULL]
);
$map->keys(); # ['a',10]
$map->values(): # [true,NULL]

use Mfn\Util\Map\SimpleOrderedValidatingMap as Map;

$map = new Map(
              function($key) {
                if ($key instanceof \stdClass) {
                  return;
                }
                throw new \Mfn\Util\Map\SimpleOrderedValidatingMapException(
                  'Only keys of type stdClass are allowed'
                );
              },
              function($value) {
                if ($value instanceof \stdClass) {
                  return;
                }
                throw new \Mfn\Util\Map\SimpleOrderedValidatingMapException(
                  'Only values of type stdClass are allowed'
                );
              }
           );
# Throws an exception
$map->add(1,2);