PHP code example of white-rabbit-1-sketch / php-file-hash-map

1. Go to this page and download the library: Download white-rabbit-1-sketch/php-file-hash-map 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/ */

    

white-rabbit-1-sketch / php-file-hash-map example snippets


use PhpFileHashMap\FileHashMap;

$hashMap = new FileHashMap(256); // Creates a hash map with a size of 256 buckets

$hashMap->set('key1', 'value1');
$hashMap->set('key2', 'value2');

$value = $hashMap->get('key1');
echo $value; // Outputs 'value1'

$hashMap->remove('key2');

if ($hashMap->has('key2')) {
    echo "Key exists!";
} else {
    echo "Key does not exist!";
}

echo $hashMap->count(); // Outputs the number of active buckets

// Iterating over keys
foreach ($hashMap->keys() as $key) {
    echo $key . "\n";
}

// Iterating over values
foreach ($hashMap->values() as $value) {
    echo $value . "\n";
}

$hashMap->clear(); // Removes all keys and values

use PhpFileHashMap\FileHashMap;

$hashMap = new FileHashMap(256, destroyDataFileOnShutdown: true); // Deletes the file on shutdown

$hashMap->defrag();

protected function serialize(mixed $data): string
{
    return serialize($data);
}

protected function unserialize(string $data): mixed
{
    return unserialize($data);
}

use Opis\Closure\SerializableClosure;

class FileHashMapWithClosures extends FileHashMap
{
    // Override the serialize method to handle closures
    protected function serialize(mixed $data): string
    {
        return \Opis\Closure\serialize($data);
    }

    // Override the unserialize method to handle closures
    protected function unserialize(string $data): mixed
    {
        return \Opis\Closure\unserialize($data);
    }
}